yesIcan
yesIcan

Reputation: 1711

how to resolve "Unable to find package" nuget error

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 ?

Upvotes: 115

Views: 309451

Answers (14)

sefak14
sefak14

Reputation: 13

dotnet nuget locals -c all

To fix the problem, firstly I run above code in terminal and then restarted the project. It worked for me!

Upvotes: 2

yesIcan
yesIcan

Reputation: 1711

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.

Upvotes: 1

David Ayres
David Ayres

Reputation: 105

I'm not sure if this will help anyone, but I had this issue after being forced to change the domain for my Windows 10 PC, which caused my Windows profile to be re-created (yes, I know there are ways around this, but due to security limitations with my company I couldn't use any workarounds). This also caused my NuGet.Config to be re-created for my new profile (don't ask how, I honestly don't remember).

The specific error I was getting when trying to run "dotnet tool install --global dotnet-ef" was:

"Package Source Mapping is enabled, but no source found under the specified package ID: dotnet-ef"

After searching and reading several different articles here on SO and elsewhere I finally figured out how to fix it for my specific situation, by putting parts of different solutions together that I found along with a bit of trial and error:

  • I'm not sure if it's neccessary or not, but I closed my IDE (Visual Studio 2022) before making these changes and then re-opened my IDE after making the changes.
  • On Windows 10/11 press your Windows key and type %appdata% and press Enter. This should open your user profile's AppData\Roaming directory (if not go to your AppData\Roaming directory).
  • Open the NuGet folder.
  • Edit your NuGet.Config file with Notepad or whatever text editor you prefer.
  • Make sure the "packageSources" and "packageSourceMapping" nodes are like the following:
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
  <packageSourceMapping>
    <packageSource key="nuget.org">
    <package pattern="*" />
    </packageSource>
  </packageSourceMapping>

And just for overkill here is my complete NuGet.Config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageRestore>
    <add key="enabled" value="True" />
    <add key="automatic" value="True" />
  </packageRestore>
  <bindingRedirects>
    <add key="skip" value="False" />
  </bindingRedirects>
  <packageManagement>
    <add key="format" value="0" />
    <add key="disabled" value="False" />
  </packageManagement>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
  <packageSourceMapping>
    <packageSource key="nuget.org">
    <package pattern="*" />
    </packageSource>
  </packageSourceMapping>
</configuration>

I understand this may only help people with the same type of issue I had, with my Windows profile being new, but I wanted to put this here just in case because I was pulling my hair out trying to get things to work with EntityFrameworkCore.

Upvotes: 1

Indigo
Indigo

Reputation: 180

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.

Upvotes: 6

sotto
sotto

Reputation: 2126

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.

Upvotes: 23

JBSnorro
JBSnorro

Reputation: 6746

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

Upvotes: 12

zille_elahi
zille_elahi

Reputation: 79

The error could be due to installation of any third party packages like Resharper which overrides the settings of visual studio or due to reset of visual studio settings.

Upvotes: 0

Serhat
Serhat

Reputation: 638

For JetBrains Rider Users; This is how you solve this problem in JetBrains Rider:

  1. Open NuGet section
  2. Add a Feed by clicking "+" sign
  3. Write Nuget in the name section, and for the URL section write this https://api.nuget.org/v3/index.json then click ok.
  4. Restart JetBrains Rider and the problem should be resolved.

enter image description here

Upvotes: 1

Shmuel Berezin
Shmuel Berezin

Reputation: 96

In addition to sotto's answer, this is the way to do it with dotnet cli:

dotnet nuget add source "https://api.nuget.org/v3/index.json" --name "NuGet official package source"

Upvotes: 6

robyaw
robyaw

Reputation: 2320

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

Upvotes: 0

Ogglas
Ogglas

Reputation: 70184

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://learn.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.

Upvotes: 1

Pooja Sinha
Pooja Sinha

Reputation: 97

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.

Upvotes: -1

jainashish
jainashish

Reputation: 5203

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 !!

Manage Nuget Packages for Solution

I am writing this answer as I tried the below solutions but none of them worked :

  1. Clearing the cache
  2. Restoring or re-installing the packages
  3. Changing the targetFramework
  4. Updating the Nuget Package manager

Solution:

Step 01. Go to Package Manager Settings (Tools > Nuget Package Manager > Package Manager Settings) 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.

enter image description here

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

enter image description here

Upvotes: 260

Israel Ocbina
Israel Ocbina

Reputation: 577

I just did this and it went fine. In your Visual Studio, go to:

  1. Tools
  2. Nuget Package Manager
  3. General
  4. Click button "Clear All Nuget Cache(s)
  5. Package Resources and click "Update"
  6. Woolah! Error gone...

Upvotes: 16

Related Questions