Manage npm packages in Visual Studio

npm allows you to install and manage packages for use in your Node.js applications. If you're unfamiliar with npm and want to learn more, go to the npm documentation.

Visual Studio makes it easy to interact with npm and issue npm commands through the UI or directly. You can use the following methods:

These features work together and synchronize with the project system and the package.json file in the project.

Install packages from Solution Explorer

The easiest way to install npm packages is through the npm package installation window. To access this window, right-click the npm node in the project and select Install New npm Packages.

Install new npm package from solution explorer

In this window you can search for a package, specify options, and install.

Search npm package

  • Dependency type - Chose between Standard, Development, and Optional packages. Standard specifies that the package is a runtime dependency, whereas Development specifies that the package is only required during development.
  • Add to package.json - This option is deprecated
  • Selected version - Select the version of the package you want to install.
  • Other npm arguments - Specify other standard npm arguments. For example, you can enter a version value such as @~0.8 to install a specific version that is not available in the versions list.

You can see the progress of the installation in the npm tab in the Output window. This may take some time.

Tip

You can search for scoped packages by prepending the search query with the scope you're interested in, for example, type @types/mocha to look for TypeScript definition files for mocha. Also, when installing type definitions for TypeScript, you can specify the TypeScript version you're targetting by adding @ts2.6 in the npm argument field.

Manage installed packages in Solution Explorer

npm packages are shown in Solution Explorer. The entries under the npm node mimic the dependencies in the package.json file.

Search npm package

Package status

  • Installed package - Installed and listed in package.json
  • Extraneous package - Installed, but not explicitly listed in package.json
  • Missing package - Not installed, but listed in package.json

Right-click a package node or the npm node to take one of the following actions:

  • Install missing packages that are listed in package.json
  • Update packages to the latest version
  • Uninstall a package and remove from package.json

Use the .npm command in the Node.js Interactive Window

You can also use the .npm command in the Node.js Interactive Window to execute npm commands. To open the window, right-click the project in Solution Explorer and choose Open Node.js Interactive Window.

In the window, you can use commands such as the following to install a package:

.npm install azure@4.2.3

Tip

By default, npm will execute in your project's home directory. If you have multiple projects in your solution specify the name or the path of the project in brackets. .npm [MyProjectNameOrPath] install azure@4.2.3

Tip

If your project doesn't contain a package.json file, use .npm init -y to create a new package.json file with default entries.