how to resolve "Unable to find package" nuget error
NugetNuspecNuget Problem Overview
Working on creating a nuget pkg for a project (A.csproj) which depends on another project (B.csprojec) added as a project reference.
Here is the .nuspec ,
<?xml version="1.0"?>
<package >
<metadata>
<id>A.Client</id>
<title>A.Client</title>
<description>HttpClient and Models for calling the A Microservice.</description>
<version>1.0.2</version>
<authors></authors>
<owners></owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<copyright>.</copyright>
<tags></tags>
<dependencies>
<group targetFramework=".NETFramework4.5">
<dependency id="Newtonsoft.Json" version="9.0.1" exclude="Build,Analyzers" />
// <dependency id="B" version="1.0.0"/> tried this but same error
</group>
<group targetFramework=".NETStandard2.0">
<dependency id="Newtonsoft.Json" version="9.0.1" exclude="Build,Analyzers" />
// <dependency id="B" version="1.0.0"/> tried this but same error
</group>
</dependencies>
<frameworkAssemblies>
<frameworkAssembly assemblyName="System.Net.Http" targetFramework=".NETFramework4.5" />
</frameworkAssemblies>
</metadata>
<files>
<file src="bin\$configuration$\netstandard2.0\A.dll" target="lib\netstandard2.0" />
<file src="bin\$configuration$\net45\A.dll" target="lib\net45" />
</files>
</package>
I used
nuget pack A.nuspec -Properties configuration=debug
To generate the package. However when I tried to consume this package inside c.csprojc, I get the following error
Severity Code Description Project File Line Suppression State
Error NU1101 Unable to find B. No packages exist with this id in source(s): Local Package source, Microsoft Visual Studio Offline Packages, nuget.org, Package source
What did I miss ?
Nuget Solutions
Solution 1 - Nuget
The package source was offline !!
I installed VS2017 professional on my system and opened an existing project and found that multiple packages were missing. I tried everything I could, without looking at Package Source !!
I am writing this answer as I tried the below solutions but none of them worked :
- Clearing the cache
- Restoring or re-installing the packages
- Changing the targetFramework
- Updating the Nuget Package manager
Solution:
Step 01. Go to Package Manager Settings (Tools > Nuget Package Manager > Package Manager Settings)
Step 02. Check the Package Source(s). As you can see, the package source is here already downloaded SDK/nugets/packages. I don't know the reason but the online package source from nuget.org was missing from my system installation of Visual Studio.
Step 03. Install the nuget.org as package source and then 'Clear All Nuget Cache(s)' and then restore the packages. The error will go away.
Name: nuget.org ( or as you wish) Source: https://api.nuget.org/v3/index.json
Solution 2 - Nuget
I just did this and it went fine. In your Visual Studio, go to:
- Tools
- Nuget Package Manager
- General
- Click button "Clear All Nuget Cache(s)
- Package Resources and click "Update"
- Woolah! Error gone...
Solution 3 - Nuget
GitHub Actions
I would like to add that in Github Actions, the nuget.org repository is not queried by default. Instead GitHub defaults to a cached package repository and it might happen that your referenced package is not in that cache but is on nuget.org (I had that for System.CommandLine).
You can fix this by adding a nuget.config
file to your root directory containing the following
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>
Example github_action.yml file
name: .NET
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 5.0
- name: Clean
run: dotnet clean && dotnet nuget locals all --clear
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
Solution 4 - Nuget
for me, %appdata%\NuGet\NuGet.Config
contained only
<activePackageSource>
<add key="nuget.org" value="https://www.nuget.org/api/v2/" />
</activePackageSource>
after doing
nuget sources add -Name "NuGet official package source" -Source "https://api.nuget.org/v3/index.json"
this was added
<packageSources>
<add key="https://www.nuget.org/api/v2/" value="https://www.nuget.org/api/v2/" />
<add key="NuGet official package source" value="https://api.nuget.org/v3/index.json" />
</packageSources>
afterwards packages were found.
Solution 5 - Nuget
I had a similar issue. It couldn't find the packages as it was looking for them stored offline in my computer instead of online. The Visual Studio NuGet settings screen didn't show this but the error message did. I have no interest in offline packages so to solve it (windows):
First go to in Visual Studio 2019, Tools>Nuget Package Manager>Package Manager Settings>Package Sources and add: https://api.nuget.org/v3/index.json (if this isn't already present), give it the name nuget.org. My error persisted after doing this, hence the next the steps.
Press start and enter %appdata% to take you to C:/Users/[username]/AppData/Roaming. Navigate to the NuGet directory and open NuGet.Config.
All Package sources appear here, even if they don't appear on the Visual Studio Package Manager settings.
Modify the PackageSources section to remove the sources you don't need. In my case I didn't need the offline source, so I was left with:
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
This solved the issue for me.
Solution 6 - Nuget
Started getting these errors sporadically for our Azure DevOps build pipeline but never locally.
Example error:
> error NU1101: Unable to find package > Microsoft.EntityFrameworkCore.Sqlite. No packages exist with this id > in source(s): Microsoft Visual Studio Offline Packages
If we restarted the build it could work but then it happened again.
I then noticed it always happened in our test project and not our main web project.
We use .NET/NuGet
pipeline caching to speed up our builds. We use the default version that looks like this:
variables:
NUGET_PACKAGES: $(Pipeline.Workspace)/.nuget/packages
steps:
- task: Cache@2
inputs:
key: 'nuget | "$(Agent.OS)" | **/packages.lock.json,!**/bin/**'
restoreKeys: |
nuget | "$(Agent.OS)"
path: $(NUGET_PACKAGES)
displayName: Cache NuGet packages
https://docs.microsoft.com/en-us/azure/devops/pipelines/release/caching?view=azure-devops#netnuget
The only change is that Microsoft uses $(UserProfile)
but we use $(Pipeline.Workspace)
for the variable. Got an error using $(UserProfile)
that you can read more about here:
https://github.com/MicrosoftDocs/azure-devops-docs/issues/10110
It reads packages.lock.json
that needs to be enabled by editing your project file (.csproj).
<PropertyGroup>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>
https://devblogs.microsoft.com/nuget/enable-repeatable-package-restores-using-a-lock-file/
Turned out we only generated a packages.lock.json
for our web project but not our test project. After adding RestorePackagesWithLockFile
to our test project as well it started working again.
Solution 7 - Nuget
I resolved this by adding dependent file
<files>
<file src="bin\$configuration$\netstandard2.0\a.dll" target="lib\netstandard2.0" />
<file src="bin\$configuration$\net45\a.dll" target="lib\net45" />
<file src="bin\$configuration$\netstandard2.0\b.dll" target="lib\netstandard2.0" />
<file src="bin\$configuration$\net45\b.dll" target="lib\net45" />
</files>
Also, don't forget to clean local cache nuget locals all –clean
or change version.
Solution 8 - Nuget
I was able to resolve this issue by adding few more package source links in nuget package source(Tools -> Nuget Package Manager -> Package Manager settings -> Package source).
Before trying above solution, I tried, clearing nuget cache, updating targetFramework, re-installing the package, updating the package manager, none of them worked.
Solution 9 - Nuget
I had a similar issue whereby I had multiple package sources defined, and api.nuget.org
was not being hit due to a failure in another source. I mitigated this by using the --ignore-failed-sources
option, e.g.:
dotnet tool install -g dotnet-ef --ignore-failed-sources
Output:
C:\Program Files\dotnet\sdk\5.0.302\NuGet.targets(131,5): warning NU1801: Unable to load the service index for source [[REDACTED]]. [c:\temp\3iqdpc5w.qpj\restore.csproj]
You can invoke the tool using the following command: dotnet-ef
Tool 'dotnet-ef' (version '5.0.8') was successfully installed.
Verified as installed via:
dotnet tool list -g
Output:
Package Id Version Commands
--------------------------------------
dotnet-ef 5.0.8 dotnet-ef