Node.js packages (npm)

Overview of NPM (Node Package Manager)

npm is a package manager that helps to organize and share Node.js packages and modules. NPM can download packages and check for updates to packages you already have installed.

This article is a quick overview on Node.js packages. For futher information, view the following page from docs.npmjs.com:

What are packages?

A package is one or more .js files (modules) grouped (or packaged) together. The files in a package are reuseable code that perform a specific function for your Node.js app. This page lists the most popular packages:

Downloading and using packages allows you to add code and functionality to your site without having to build it yourself.

Where to find packages

You can browse packages on the Node.js website:

Local or global installation?

You can install packages locally or globally. It depends on how you want to use the package.

Local

The default behaviour of running npm install is to install locally. This is preferred if you're going to use require to depend on the package from your own module.

Globally

If you want to use the package as a command line tool.

Checking which packages are installed

Local packages

To check which local packages are installed, navigate to your site's application directory where the /node_modules folder resides. Then run the following:

[server]$ ls node_modules

Global packages

Run the following:

[server]$ npm ls -g --depth=0

Installing packages

View the following page for instructions on how to install packages locally or globally:

Updating packages

Updating local packages

Navigate to your site's application directory where the local /node_modules folder resides.

First, check which packages need to be updated:

[server]$ npm outdated

You can update a single package with:

[server]$ npm update -S <package_name>

You can update all local packages with:

[server]$ npm update -S

It's a good idea to use the -S flag. This saves the new version as the minimum required dependency in your site's package.json file. If you do not use the -S flag, the package will update, but the package.json file for your site will not be updated.

Updating global packages

First, check which packages need to be updated:

[server]$ npm outdated -g --depth=0

You can update a single package with:

[server]$ npm outdated -g <package_name>

You can update all global packages with:

[server]$ npm update -g

Using a package to update packages

There are several packages that can help to update your site's packages. For example:

See also

Did this article answer your questions?

Article last updated .