Many times, there is a need to update all packages within a project to their latest versions. Each project has dependencies crucial for its functionality, and over time, these dependencies release new versions. In a project, you may want to check and update all dependencies to their latest versions.

There are multiple ways to update all dependencies in a package.json file to their latest versions.

  • npm outdated and update commands This command reads the dependencies and their versions from the package.json file. It checks and identifies the latest versions for each dependency, providing a list that includes both the current version and the latest version for each dependency.
E:\myapp>npm outdated
Package       Current   Wanted   Latest  Location                   Depended by
autoprefixer  10.4.16  10.4.17  10.4.17  node_modules/autoprefixer  myapp

For example, in this project, the current version of autoprefixer is 10.4.16, while the latest version is 10.4.17.

The outdated command provides a report containing version information, but it does not update the project.

Next, you can use the npm update command to update all dependencies to their latest versions.

E:\myapp>npm update
added 1 package, changed 14 packages, and audited 693 packages in 1m

140 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

This command performs the following actions:

  • Updates the version in package.json to the latest version.

  • Installs the new version of dependencies into node_modules.

  • Updates the version and the versions of indirect dependencies in package-lock.json to the latest versions

  • use npm-check-updates package

npm-check-updates is a third-party tool to manage and install dependencies. It provides the ncu command to update all dependencies to their latest versions.

First, install globally using the below command

npm install -g npm-check-updates

After installation, the ncu command works.

Running the ncu command without options lists the current and latest versions of all dependencies.

ncu

Using the ncu -u command option updates all packages to their latest versions in the package.json. However, it does not install the latest version of dependencies.

ncu -u

if you want to update a single package use 1 option with package name

ncu -f package -u

To install the latest version, the npm install command must be executed.

npm install