Overview
You can install and update Node.js packages on a DreamHost server using npm — installing them either locally in your site's application directory or globally for command line utilities.
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.
How do I install packages?
You can install packages locally or globally after logging into your server via SSH.
How do I install a package locally?
To install a package locally:
- Navigate to your site's application directory (not the /public directory).
- 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.
- Check to confirm it was correctly installed by listing the contents of the local /node_modules directory:
[server]$ ls node_modules | grep lodash lodash
How do I install a package 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
How do I update 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:
How do I 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.
How do I 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