Node.js - update all dependencies to latest version
In this article, we would like to show you how to update all dependencies to the latest version in Node.js.
Quick solution:
npm update
or:
npm update "package-name"
1. Update all dependencies
1. Check if there are dependencies that are out of date.
npm outdated
Note:
If there are any outdated dependencies, they will be listed out.
2. Update all dependencies.
npm update
Note:
This will update the packages in the node_modules folder. The package.json and package-lock.json files will be updated too.
3. If you don’t want to update all packages, you can specify the package names you want to update at the end of the command.
npm update "dotenv"
2. Update all dependencies with major changes
In this example, we present how to update dependencies when there has been a major version change.
1. Check the changelog of the package for breaking changes that could affect your application.
2. If it's safe to update, run the following command:
npm install <packagename>@latest
Practical example:
npm install dotenv@latest
3. Verify if the app isn’t broken and repeat the process for other packages where there is a major version change.
3. Alternative solution
1. Check for updates using npm-check-updates
tool:
npx npm-check-updates -u
Note:
This command will check for updates for all of the dependencies including major version changes in the
package.json
file.
2. If you agree for the updates run the following command:
npm install