Add a NuGet package to a VS Code project

This article demonstrates how to add a NuGet package to a Visual Studio Code project. You can also find here how to install the NuGet Manager on your machine.

In the same way that a modern builder employs ready-made modules to construct a building, we use packages for our development work. Package managers are used to manage the packages. NuGet is the most popular of them.

To know more about NuGet, follow the link.

Install NuGet Package Manager

This section describes how to install NuGet Manager on your computer globally or as a Visual Studio Code extension.

Ensure that Visual Studio Code and the NuGet CLI tool are both installed and up-to-date on your PC. If not, go here to download VS Code. Run the following command to ensure you have the most recent NuGet CLI tool version installed:

nuget update -self

To accomplish the same task in Linux, run the following command (which copies nuget.exe to the bin folder):

sudo apt install nuget

Another way to use the NuGet package manager is to install the NuGet Package Manger GUI for VSCode extension in Visual Studio Code:

  • open VS Code
  • click on the button Extensions (or strike key combination Ctrl+Shift+X)
VSC Button Extensions
  • Enter the text NuGet Package Manager GUI in the search field:
VSC Nuget Install

and install the package.

Add package to a project

The section describes how to add a package to a project from NuGet.org or local repository

Create a console-type project in Visual Studio Core. For further information, see the link.

In order to add a package, run the following command on your console:

dotnet add library package package_name

Example:

  • Navigate to the parent folder of the project (e.g. if the project folder is c:\vsc\vsc-dotnet-core, you should navigate to c:\vsc)
  • Enter the command in the terminal:
dotnet add vsc-dotnet-core package CsvHelper

In this case, the project folder is vsc-dotnet-core, and CsvHelper is the installed package.

The following information is written to the project file (in our case, vsc-dotnet-core.csproj) after the package installation is complete:

<ItemGroup>
  <PackageReference Include="CsvHelper" Version="27.2.1" />
</ItemGroup>

If you have a problem with the packages (they are missing, they need to be updated, or the source code has been moved to a different location), run the following command:

dotnet restore

Run the following command to verify whether the project is operational:

dotnet run

You anticipate the following outcome:

Console

If you have a ready-made package configuration file packages.config with the following content:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="CsvHelper" version="27.2.1" targetFramework="net5" />
</packages>

you can run the following command to install all of the packages listed in it:

nuget install packages.config -OutputDirectory packages

where packages is the directory for the project dependency packages.


Was this helpful?

1 / 1

Leave a Reply 0

Your email address will not be published. Required fields are marked *