Node.js packages (npm)

 

Overview

This article is a quick overview of npm and how to use it at DreamHost.

Background

npm is a package manager that helps to organize and share Node.js packages and modules. It can download new packages and check for updates to existing packages that are already installed.

Prerequisites

To follow the steps in this article you must first manually install a custom version Node.js under your server's username. This also installs npm, allowing you to run the commands below.

FAQs

What are packages?

A package is one or more JavaScript files (modules) grouped (or packaged) together. The files in a package are reusable 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 do I find packages?

You can browse packages on the Node.js website.

Should I install locally or globally?

It depends on how the package will be used.

  • Locally — This is generally preferred if you plan to use the package as a dependency in your own module.
  • Globally — If you want to use the package as a command line tool.

Using npm

The following are examples of the basic npm commands you can use to update and install packages.

Update npm

Check the version using -v.

[server]$ npm -v

Update npm to the latest version.

[server]$ npm install npm@latest -g

Check which packages are installed

Check the directory npm writes to.

[server]$ npm config get prefix
/home/username/.nvm/versions/node/v20.18.0

If you installed NVM using the instructions above, you'll see the directory response as the /.nvm folder under your current user.

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 from any directory:

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

Install and update packages

See this article for instructions on how to install and update packages locally or globally.

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?