Updating NPM

October 17, 2024

By John Curry

This tutorial is a work in progress, dedicated to helping software developers understand how to manage NPM (Node Package Manager) updates. It’s helpful in updating NPM itself, Node, and updating specific NPM packages both globally and within an application.

Which node version is used in an app?

Unfortunately figuring out which version an app isn’t obvious. There could be information about which engine is used in your package.json file, but it doesn’t look like there’s a simple direct method, which is why I like to use a .nvmrc file to keep track of which version is used.

First, make sure the app works with whatever version of Node.js you’re currently using, then once it works, run the below command (in the root of your project) to declare which version the app uses:

$ node -v > .nvmrc

This will take the current version of Node and put the output in a .nvmrc file in your project. From there you can keep it in your version control and change the file as you upgrade versions.

Updating Node Version:

Hopefully you’re using NVM (Or NVM for Windows) to manage the version of Node.js and NPM running on your computer. If you are, then updating Node.js is as simple as running the following commands:

$ nvm install lts # Install the latest stable version of node.
$ nvm use lts # Use the latest 'stable' version of node.
$ nvm list # list nodejs installations.
$ nvm list available # lists of versions available for download
$ nvm use 22.9.0  # sets 22.9.0 as the currently in use Node version.
$ node -v # Checks the current version of Node.js running.

Once you have the version of Node.js you want on your computer set up and in use, now you can install and update packages globally and within an app.

Quickly Upgrading an App using NPM:

If you want to rush the job of upgrading Node.js packages in an app, you can do so quickly by updating all packages in a package.json file at one time with the following commands:

$ npm install -g npm-check-updates # installs updater package globally for the current Node version
$ npx ncu # Lists all outdates packages in an app
$ npx ncu -uf react-router-dom # Updates react-router-dom (or other package)
$ npx ncu -u # Updates all Node.js packages within the app in the package.json.
$ npm install # Installs updates from the package.json file.

Carefully Updating an app using Node.js

If you have multiple major version updates to do you might run into breaking changes, which can be hard to track down if you’ve got breakages from multiple packages. If a mass update gives you problems, you may want to upgrade a few packages or even one NPM package at a time. You can do that with these commands:

$ npm install -g npm-check-updates # installs updater package globally for the current Node version
$ npx ncu # Lists all outdated node packages
$ npx ncu -uf <package-name> # Updates individual package to latest.

If you’re REALLY having a hard time, you may want to sequentially update the packages one at a time, one version at a time. For example, if stripe-js is at version 4, and you haven’t touched the project in a few years, and it’s at version 2, you may want to update to version 3, then test the app, then update to version 4. If that’s the case, then $ npx ncu lists all the out of date packages, and you can just look through the version history of that package and decide how many jumps you’d like to make to get up to date by installing specific versions of that package.

Installing specific versions of a package:

Appending @version-number at the end of the package will install a specific version of that package, as seen in the example below:

npm install @headlessui/react@2.1.10

Upgrading NVM:

coming soon…