Node.js — installing packages

 

Overview

This article describes how to install Node.js packages to a DreamHost server.

Installing Node.js and npm

Node.js is not installed by default on newer DreamHost servers. You must manually install a custom version under your server's username. See this article for instructions on how to install a custom version of Node.js.

Once installed, npm is also installed, and you'll be able to run the commands below.

Installing packages

You can install packages locally or globally after logging into your server via SSH.

Install locally

To install a package locally:

  1. Navigate to your site's application directory (not the /public directory).
  2. Install a package using this syntax:
    [server]$ npm install <package_name>

    For example, this installs a package named 'lodash':

    [server]$ npm install lodash

    A directory is created in your site's application directory named /node_modules.

  3. Check to confirm it was correctly installed by listing the contents of the local /node_modules directory:
    [server]$ ls node_modules | grep lodash
    lodash

Install globally

If your project depends on a specific package, it should always be installed locally, as shown above. Global installs should be reserved for packages that your projects do not depend on, such as CLI utilities. 

Use the -g flag to install a package globally:

[server]$ npm install -g <package_name>

You can confirm which packages are installed globally by running the following:

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

Updating packages

The process of updating a package is slightly different depending on if it was installed locally or globally.

Using a package to update packages

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

Update 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

Update a single package

You can update a single package with:

[server]$ npm update -S <package_name>

Update all packages

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 will not.

Update global packages

First, check which packages need to be updated:

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

Update a single package

You can update a single package with:

[server]$ npm outdated -g <package_name>

Update all packages

You can update all local packages with:

[server]$ npm update -g

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?