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 having a hard time or haven’t updated in a long time, you may want to incrementally update each package. For example to get from version 2 to version 4 in Stripe, it may help to upgrade to version 3 before getting fully updated. You can do that by installing the specific version you want to jump to.
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…