Skip to main content
Photo by Paul Esch-Laurent via Unsplash

Update Npm Packages in All Available Nvm Environments

If you are like me, then you are probably using nvm to manage your node environments. I have a few projects that are using different node versions, and I have to switch between them from time to time. Sometimes then I forget having switched and type some globally installed npm command and end up, not having it installed in the current environment or not updated to the latest version.

This lead to plenty of confusion and time spent to debug issues that would have not existed else. So I decided to write a small bash script that will update all globally installed npm packages in all available nvm environments I have set up. I also added a cronjob to run this script every night, because I don’t want to think about it anymore.

Sidenote: I am pretty sure there is a baked-in way in nvm to accomplish the same with a small node script. But I went the bash route, because I am more comfortable with it and don’t want to use up any more time with this.

This script works for me and my setup (Ubuntu 22.04 with bash). It might not work for you, but it should be easy to adapt it to your setup.

First, here is the script in its whole glory (don’t just copy and paste, read on for the explanation, because there are some built-in things in this script that depend on my own setup):

 1#!/bin/bash
 2
 3echo "##########################################################################"
 4echo "starting update-npm.sh"
 5echo `date`
 6echo "##########################################################################"
 7
 8# exit if any command fails
 9set -e
10
11# Load nvm
12export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
13[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
14
15for DIRNAME in /home/patrick/.nvm/versions/node/*/; do
16
17    DIR=$(basename "$DIRNAME")
18    nvm use $DIR
19
20    # update global npm packages
21    npm --no-fund --no-audit --quiet -g install \
22        svgo cypress typescript \
23        @davidsneighbour/remark-config \
24        @socketsecurity/cli \
25        bun
26
27done
28
29echo "##########################################################################"
30echo "done with update-npm.sh"
31echo `date`
32echo "##########################################################################"
33
34nvm use

Now a quick breakdown of what this script does:

Line 12: This line loads the nvm environment by setting the NVM_DIR environment variable. This line is listed at the end of installing nvm to your system and depends on your own setup. Have a look in your .bashrc if you don’t already know where to look for it.

1export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
2[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

Line 15: The script then iterates through all installed Node.js versions managed by nvm in the specified directory (/home/patrick/.nvm/versions/node/*/). This path will be different on your system, so make sure to change it to the correct path. Because I am using the script as a cronjob, I can’t replace /home/patrick/ with {$HOME} or ~. You could, if you run the script manually only. This would make it more portable but non-working in cronjobs.

For each version, it switches the active Node.js version to that version using nvm use $DIR.

1for DIRNAME in /home/patrick/.nvm/versions/node/*/; do
2    DIR=$(basename "$DIRNAME")
3    nvm use $DIR

Line 21: Within the loop, for each Node.js version, it updates a specific set of global npm packages. The npm --no-fund --no-audit --quiet -g install command is used to install/update the specified packages globally without showing funding information, auditing for vulnerabilities, or printing output to the console unless there is an error. I am doing this, because in cronjobs I filter the output into a log file and don’t want to have any unnecessary information in there.

You could add anything you would want to be installed or configured on each version of node you have installed.

1    npm --no-fund --no-audit --quiet -g install \
2        svgo cypress typescript \
3        @davidsneighbour/remark-config \
4        @socketsecurity/cli \
5        bun

Line 31: In the end the script resets the active Node.js version to the default using nvm use. For the cronscript this line is not required, but I added it, because I might call the script manually from time to time. In that case, it’s always good to end up on the same version I started with. If there is no version set in the directory you are currently in, nvm will iterate up to your default version that you have set in your .nvmrc file.

1nvm use

That’s it. This script is useful for me. It might be useful for you.

The cronjob: I added this script to my crontab with the following line:

10 0 * * * /path/to/update-npm.sh >> /home/patrick/cron.log 2>&1

In this cronjob:

  • 0 0 * * * specifies the schedule: at midnight, every day.
  • /path/to/update-npm.sh is the path to the script.
  • >> /home/patrick/cron.log appends the output to a log file in my home directory.
  • 2>&1 directs both standard output and standard error to the log file1.

I can then check the log file for errors and see if the script ran successfully.


  1. If you wish to learn more about the “redirection” syntax then have a look at my previous post about this topic↩︎

Back to top
Back Forward