91

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 ?

yesIcan
  • 1,441
  • 1
  • 11
  • 18
  • Did you build package B? The error suggests that package B is not built or not available from the directory containing package A. The `nuget pack` command you used only mentions A.nuspec. – Matt Ward Sep 18 '18 at 10:01
  • a and b.csproj are in the same solution, a referenced b as a project, not a package. – yesIcan Sep 18 '18 at 16:37
  • Did you try the `-IncludeReferencedProjects` option when using `nuget pack`? – Matt Ward Sep 19 '18 at 12:20
  • 1
    Set the package source url : https://api.nuget.org/v3/index.json under Tools -> Nuget package Manager -> package manager settings (Package source tab) – Pranesh Janarthanan Jun 05 '21 at 14:42

12 Answers12

224

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

jainashish
  • 4,702
  • 5
  • 37
  • 48
  • 14
    This was exactly my solution. I don't know why the VS installer would have excluded nuget.org url as a package source, you would think it would have been included as the default source. – Skystrider Feb 25 '20 at 15:39
  • 3
    Thank you. This was a problem for me and you pointed to an exact solution for me, I suffered a few days until I find this page. – RotatingWheel Apr 07 '20 at 17:32
  • 1
    Thank you, thank you. I'm just getting back into Visual Studio after a few years, and had a couple of these errors. Found a few answers I couldn't understand or that seemed like bad workarounds. Found yours, and it makes sense and worked! Now I can actually start coding! – Doug Glancy May 04 '20 at 15:40
  • 1
    For anyone encountering this problem on an Azure build, this open Azure DevOps issue #9046 provided an answer: https://github.com/NuGet/Home/issues/9046#issuecomment-579012605 In short, adding a nuget.config file in your project with `` before your package source forces the build process to download it again, preventing this "offline package" error. – Dan Oliver Jul 08 '20 at 14:39
  • 4
    Bingo. This is really unacceptable MS teams for bizarrely not having nuget source set. I hope this is just a bug, but its 2021 and i'm commenting on 2019 comments. As an experienced .NET dev, on a needed full system reinstall, fully installed VStudio 2019 Pro newest version, manually installed .NET Framework 4.8 and dotnet core 5.0 and 3 versions (dot.net), but still had this problem?! How could this be ok? It's unbelievable that they somehow had this setting by default. Sorry to complain, but it's not good for adoption. – Nicholas Petersen Apr 14 '21 at 15:13
  • @Skychan I think VS installed it for the current user but you opened VS as Administrator and you have to set those by yourself. – Chsiom Nwike Jul 06 '21 at 07:56
  • 1
    Thank you! I suffered the same problem. And your solution works well. –  Jul 16 '21 at 01:41
  • 1
    I faced the same issue in VS 2019. Wonder why there are no clear instructions by MS. – Rehan Arshad Jul 18 '21 at 21:05
  • 1
    Thank you! Just reinstalled VS2022 and my new projects weren't reading packages. This fixed it! – snejame Dec 01 '21 at 10:54
  • 1
    Thank a lot ! It did work on my computer – Axel Samyn Jul 12 '22 at 11:54
  • 4
    worked in VS2022 Nuget package manager – coder kemp Jul 25 '22 at 17:43
  • 2
    This was my issue with a clean install of VS 2022 on a new windows 11 dev machine. – F Dev Oct 23 '22 at 23:21
  • 2
    I got the same issue for VS 2022. Thank you @jainashish for providing this step-by-step resolution. – Rodrigo Nascentes Nov 16 '22 at 18:09
  • why the hell did i have to do this? – user433342 Mar 16 '23 at 22:53
  • 1
    I just cannot believe in 2023 with VS 2022 met the same issue back in 2019. – NanoNova Jun 18 '23 at 14:56
16

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.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
sotto
  • 2,044
  • 4
  • 18
  • 22
13

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...
Israel Ocbina
  • 517
  • 8
  • 14
10

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
JBSnorro
  • 6,048
  • 3
  • 41
  • 62
  • This stumped me for a while as I had a matrix build of `ubuntu-latest` and `windows-latest` and the Linux one was working perfectly fine with NuGet but not the windows one! – Kevin Smith May 03 '21 at 22:27
  • This helped me a lot. Thank you. I needed a solution for github actions and the restore step was the solution. – Tanyo Ivanov May 08 '23 at 13:09
6

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.

Indigo
  • 180
  • 2
  • 12
4

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"
1

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.

Ogglas
  • 62,132
  • 37
  • 328
  • 418
1

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

Serhat
  • 216
  • 2
  • 17
0

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.

yesIcan
  • 1,441
  • 1
  • 11
  • 18
0

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
robyaw
  • 2,274
  • 2
  • 22
  • 29
0

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.

zille_elahi
  • 39
  • 1
  • 4
-1

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.

Pooja Sinha
  • 89
  • 1
  • 5