How to Install and Use the Node Package Manager (NPM) on Linux
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
The Node Package Manager (NPM) is the most widely-used package manager for JavaScript projects and includes Node.js by default. This guide walks you through installing NPM and getting started using it on your Linux system.
What is NPM?
NPM is the default package manager for Node.js projects. NPM includes a command-line tool (CLI) that give you access to the
NPM package registry. The registry stores the numerous JavaScript packages made available through the NPM CLI, along with their metadata. The
NPM website gives you an easy way to search for JavaScript packages and read information about them. The package.json
file that is generated by the NPM CLI helps you manage project dependencies. It also ensures consistent project installations across environments.
NPM vs Yarn
The Yarn package manager has become a popular alternative to NPM. Both NPM and Yarn use the NPM registry to get packages and package information, and the commands in each tool are pretty similar.
One advantage to NPM is that it is the default package manager for Node.js. If you have Node.js installed on your system, you automatically have access to NPM. This means that once you have Node.js installed, you immediately have access to all the functionality available through the NPM package registry.
NPM is also the most popular Node.js package manager. With that popularity comes a wide range of coverage. You are more likely to find Node.js projects and examples that use NPM than you are to find ones using Yarn.
Yarn was originally designed to address performance and security concerns in NPM. And while Yarn still outshines NPM in terms of speed, NPM has made vast security improvements that put it about even with Yarn.
NoteYou can learn more about Yarn in our How to Install and Use the Yarn Package Manager guide.
To learn how to install Node.js, jump to the How to Install NPM section of this guide.
How to Install or Update NPM
The steps in this section show you how to install NPM along with Node.js. It also gives you methods for updating your NPM installation.
How to Install NPM
Since NPM is packaged with Node.js, you just need to install Node.js. The installation path recommended by NPM is using a the Node Version Manager (nvm). This version manager helps you avoid permissions issues and version conflicts with NPM packages. To install nvm follow the steps in our How to Install and Use the Node Version Manager NVM guide.
Using nvm, you can install the current stable version of Node.js, and its accompanying version of NPM using the following command:
nvm install node
To verify your NPM installation, check for the installed npm version.
npm -v
7.20.3
How to Update NPM
When working with nvm to manage your Node.js versions, updating your NPM version requires that you update your Node.js version. To make sure you are on the latest version of NPM, use NVM’s install
command to install the current stable Node.js version.
nvm install node
Then, tell NVM to use the latest version.
nvm use node
If you want to update NPM, without updating Node.js, use nvm’s dedicated command.
nvm install-latest-npm
How to Install Packages with NPM
The sections that follow show you how to install and use NPM packages.
Most often, you install NPM packages for a specific project. Use commands below to create an example NPM project to follow along with this guide.
Create a new directory for the example project and move into the new directory.
mkdir ~/example-app
cd ~/example-app
Initialize the new NPM project.
npm init
NPM prompts you for information about your project. Use the defaults for this example. The result is an initial package.json
file representing your project.
Install Packages to a Project
NPM provides two main ways for installing specific packages to your project.
You can install a specific package with NPM’s
install
command. In the example below, NPM installs the latest stable version of the package. The example installs the Express web application framework.npm install express
Alternatively, you can explicitly specify the package version you want installed on your system.
npm install express@4.17.1
You can even specify a version range for the package you want to be installed. Place the version expression between quotes, and precede the version number with the comparison operator you want to use. You can use multiple version constraints, separating them with spaces.
npm install express@">=4.1.0 <4.17.1"
The above command installs the latest available version of the Express JS package that is equal to or greater than 4.1.0 and less than 4.17.1.
Package.json
The npm init
command creates a package.json
file in the project’s base directory. Any packages installed in the project are reflected in this file. The package.json
is a reflection of your project’s dependency structure.
The file below is an example package.json
that results from the basic npm install express
command (from the above section).
- File: package.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14
{ "name": "example-app", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "express": "^4.17.1" } }
Notice that the express
package is listed under dependencies
. The ^
indicates that a package version compatible with version 4.17.1 must be installed for this project.
Take a look at NPM’s package.json
documentation, specifically the
dependencies section for more information about the syntax used for parsing package versions. The same syntax can be used when specifying a version, or a version range, for the install
command.
Install Dependencies for an Existing Project
You may come across an existing project, with its own package.json
, that you want to get up and running. Typically, you first need to install the project’s dependencies. You can do this by running the install
command from that base directory without specifying a package. For example:
npm install
NPM uses the project’s package.json
to determine which packages are needed and which versions of those packages are compatible with the project. If the project has a package-lock.json
, NPM also uses that to further ensure compatibility with the dependencies that get installed.
Install Packages Globally
You may want to install some packages globally. Packages installed in this way are available anywhere on the system, not just within a particular project.
You can accomplish this with NPM’s global (-g
) flag, as shown below:
npm install -g express
You can also use the -g
option with the commands in the next two sections. This option allows you to uninstall and update global packages.
How to Remove Packages with NPM
You can uninstall an NPM package with the uninstall
command.
npm uninstall express
The uninstall command updates the package.json
to reflect that the project no longer depends on the uninstalled package.
- File: package.json
1 2 3 4 5 6 7 8 9 10 11
{ "name": "example-app", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" }
How to Update Packages with NPM
You can use the command below to update all of a project’s packages to their latest compatible versions.
npm update
NPM references the version constraints given in the project’s package.json
file to ensure that updates do not contradict your project’s specified version.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on