How to Update All the NPM Dependencies in a Node.js Project

Do you need to update all of the NPM package dependencies in the package.json
file for your Node.js application?
When you install an NPM package dependency for your Node.js project, the latest version of that package will be installed (unless you specify otherwise). And the package and it's sub-dependencies are placed in a node_modules
directory in the root of your project folder and are also listed in your package.json
and package-lock.json
files.
Using NPM, you can run the npm update
command when inside your Node.js project. This will update all the packages listed to the latest version.
npm update
But if any of the updates are major releases, npm update
won't update those because they may contain breaking changes to your application.
To help with these kinds of updates, we can use the npm-check-updates npm package. This package will only update the dependencies listed in your package.json
file, so we'll have to run the npm install
command after we use the npm-check-updates
package.
Install the package globally on your machine with this command:
npm install -g npm-check-updates
When that's done installing, we can use the npm-check-updates ncu
command anywhere on our machine.
To update the versions of the dependencies
and devDependencies
listed in your package.json
file, execute this command in the root of your project directory:
ncu -u
You should see an output similar to this on your command line:
Upgrading /path/to/package.json
[====================] 11/11 100%
disqus-react ^1.0.5 → ^1.0.7
express ^4.14.0 → ^4.17.1
next ^8.1.0 → ^9.0.7
react ^16.8.6 → ^16.10.1
react-dom ^16.8.6 → ^16.10.1
Run npm install to install new versions.
Notice that the different types of updates are colored differently: green, blue, or red.
Open your package.json
file and notice that any outdated package versions now have the newest version listed.
Execute this command to install those updates and replace the old versions:
npm install
When the new packages are done installing, test out your application to make sure everything still works correctly. Some of the package updates may have breaking changes in them.
Hopefully, this was helpful in your Node.js project.
Thanks for reading and happy coding!