.NET NuGet Package Restore Error Fix
In this tutorial, you'll learn about .net nuget package restore error fix. We cover key concepts, practical examples, and best practices.
The Problem
Your .NET project fails to restore packages:
NU1101: Unable to find package Newtonsoft.Json. No packages exist with this id in source(s): nuget.org
NuGet restore errors happen when packages cannot be downloaded, source feeds are unreachable, or version constraints conflict.
Quick Fix
Step 1: Clear NuGet cache
WRONG -- assuming cache is always correct:
dotnet restore
RIGHT -- clear cache then restore:
dotnet nuget locals all --clear
dotnet restore
Step 2: Check NuGet sources
WRONG -- using default sources without verification:
dotnet nuget list source
RIGHT -- ensure nuget.org is enabled:
dotnet nuget enable source nuget.org
dotnet restore
Step 3: Fix version conflicts in project files
WRONG -- conflicting package versions across projects:
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="SomeOtherPkg" Version="2.0.0" />
RIGHT -- use CentralPackageManagement or align versions:
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SomeOtherPkg" Version="3.0.0" />
Step 4: Handle private feed authentication
WRONG -- no credentials for private feeds:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="PrivateFeed" value="https://pkgs.dev.azure.com/..." />
</packageSources>
</configuration>
RIGHT -- add credentials via environment variable:
dotnet nuget add source https://pkgs.dev.azure.com/... --name PrivateFeed --username PAT --password $AZURE_DEVOPS_PAT --store-password-in-clear-text
Step 5: Use lock files for deterministic restore
WRONG -- no lock file, builds differ across machines:
<PropertyGroup>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>
RIGHT -- check in packages.lock.json to version control:
dotnet restore --locked-mode
Prevention
- Add a
nuget.configto your repository with all required sources. - Use
Directory.Packages.propsfor centralized version management. - Run
dotnet restorewith--locked-modein CI to detect version drift. - Set up a local NuGet cache server for frequently used packages.
- Use
dotnet nuget locals all --clearin your CI pipeline to avoid stale caches.
Common Mistakes with nuget error
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging
These mistakes appear frequently in real-world DOTNET code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro