NuGet has problems with missing packages, how to restore?

21,461

Solution 1

Use Package Manager Console in Visual Studio to run this command.

1.This will restore all packages from solution

nuget restore YourSolution.sln

2.If you want to reinstall the packages to the same versions as were previously installed

Update-Package -reinstall

Solution 2

Honestly, whoever developed the NuGet command for VS needs to go back to the drawing board. They totaly missed the fact that sometimes these DLL(s) and/or files get corrupt or deleted. a "NuGet Get-Packages -Force" option would really save their bacon. The only GAP I see is that VS and the Package console does not allow you to invoke a forced download from NuGet. Even clearing the cache via VS is useless.

Solution 3

I ran into this issue when I tried to build my project on a computer where the packages-folder did not already exist in the solution root.

When the project file was initially created, it seems VS2019 added the following into the project file:

<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props'))" />
</Target>

From what I understand those settings are deprecated nowadays, no idea why VS inserted it in the first place.

Anyway, after removing those lines VS restored the packages and built the solution correctly again.

Solution 4

The problem with the functionality of the NuGet package can also be caused by the fact that NuGet package is requiring certain dependency that the project does not meet.

NuGet package can have a declared dependency in Dependecies section, e.g. .NETFramework,Version=v.4.6.2 while the project is targeted to an older version (Target framework: .NET Framework 4.6.1.).

Instead of the NuGet system notifying the user of this fact, the project simply does not compile.

Solution 5

It's probably a good idea to clear the Nuget Cache by deleting the contents within this directory: C:\Users\{your_username}\AppData\Local\NuGet

Share:
21,461
Thijser
Author by

Thijser

student of computer science. Currently working on my master, mostly interested in deep learning and computational intelligence.

Updated on September 04, 2021

Comments

  • Thijser
    Thijser over 2 years

    When I try to compile my program I get the following error:

    This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=317567.    
    

    Now when I right click on the solution and press

    All packages are already installed and there is nothing to restore.
    

    I tried manually reinstalling every package which didn't solve the problem, I tried reinstalling NuGet but that didn't help either and I even tried reinstalling visual studio.

    I also tried moving the package folder from the tfs folder to overwrite my package folder but that didn't solve anything. I also tried redownloading them with this package missing, that didn' t solve the problem either.

    Anybody know how to restore the nuget packages?

  • Thijser
    Thijser almost 8 years
    nuget restore TestEnvi.sln give me the error The term 'restore' is not recognized as the name of a cmdlet, function, script
  • Thijser
    Thijser almost 8 years
    I tried that but it didn't solve the problem (even after rebooting VS)
  • Thijser
    Thijser almost 8 years
    That suggests that installing nuget commands would be simply running Install-Package NuGet.CommandLine but that gives me 'Package 'NuGet.CommandLine.3.4.3' already exists in project 'TestEnvi''
  • Rahul Nikate
    Rahul Nikate almost 8 years
    Did you try Update-Package -reinstall command in package manager console
  • Thijser
    Thijser almost 8 years
    That runs for a bit and then throws the error ' The item 'packages.config' cannot be deleted '
  • Rahul Nikate
    Rahul Nikate almost 8 years
    There's one work around for this. In your solution folder there should be a "packages" directory. Open this directory and delete all the directories and then run again reinstall.
  • Thijser
    Thijser almost 8 years
    Hmm running it a second time it did complete, but I still canot run the restore command (nuget not recognized) and the problem persitsts.
  • Rahul Nikate
    Rahul Nikate almost 8 years
    Try deleting whole packages folder from solution and then run Update-Package -reinstall
  • Thijser
    Thijser almost 8 years
    An unexpected error is keeping you from deleting the folder. if you continue to receive this error you can use the error code to search for help with this problem. Error 0x8007000F: The system cannot find the drive specified. So I cannot delete the folder in question
  • Rahul Nikate
    Rahul Nikate almost 8 years
    I doubt you have file access permissions issue or corrupted drive. You need to check your OS and computer system. It has nothing to do with nuget
  • Thijser
    Thijser almost 8 years
    Any advice how to deal with these? The file sits on an veracrypt encrypted file so shouldn' t that sort of auto detect problems?
  • Rahul Nikate
    Rahul Nikate almost 8 years
    You need to check your OS and computer system. It has nothing to do with nuget. sorry its out of my knowledge.
  • Thijser
    Thijser almost 8 years
    I will if it turns out that this is indeed the problem. I'm currently looking into the filesystem. But it looks like this is going to help seeing as how there are 3 files that cannot be removed as a admin cmd.
  • Thijser
    Thijser almost 8 years
    I managed to remove the packages folder (had to specify thoese 3 files by hand) and when ran the program which resulted in the package folder being downloaded again. However the problem remains unchanged.
  • Dai
    Dai about 7 years
    The command nuget restore mysolution.sln doesn't work for me in the Package Manager Console window, I get this error: "PM> nuget restore reco.sln nuget : The term 'nuget' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."
  • Legends
    Legends over 6 years
    This worked partly, I also had to add references manually, it was a mix of all the steps
  • Nick G.
    Nick G. over 6 years
    Thank you, 1st running this : Install-Package NuGet.CommandLine then the above did the trick for me
  • Mardoxx
    Mardoxx over 5 years
    Too frigging right. One workaround for this is to uninstall the packages form your project then reinstall them. Sucks.
  • Kellen Stuart
    Kellen Stuart almost 5 years
    @NickG. That didn't work either. Maybe Install-Package NuGet ?
  • Rich
    Rich over 4 years
    It's the year 2020 and this is still broken
  • Patrick Tucci
    Patrick Tucci almost 4 years
    Thanks, this solved it for me. No clue why this reference was placed in my project file.
  • Manuzor
    Manuzor over 2 years
    2022 and counting.
  • Pankaj Kaushik
    Pankaj Kaushik over 2 years
    'Update-Package -reinstall' helped me. Thanks a lot.