NPM: How to Upgrade All Packages to the Newest Versions

Updated: December 30, 2023 By: Guest Contributor Post a comment

Explore some different ways to upgrade all packages to their latest versions in JavaScript/TypeScript projects (Node.js, React, Angular, etc).

Using npm-check-updates

This solution upgrades outdated packages by using a specific npm module. Here’re the steps:

  1. Install the npm-check-updates package globally: npm install -g npm-check-updates
  2. Run the tool to check for updates: ncu
  3. Upgrade your package.json to the latest versions: ncu -u
  4. Install the updated packages: npm install

TL;DR:

npm install -g npm-check-updates
ncu
ncu -u
npm install

Pros: Simple and straightforward to use. Cons: Requires an additional global package to be installed.

Using npm outdated and npm update

A native approach that utilizes npm’s built-in commands for checking and updating packages.

  1. Find outdated packages: npm outdated
  2. Update packages to their latest version: npm update

TL;DR:

npm outdated
npm update

Pros: No additional packages are needed. Cons: Might not update to the absolute latest versions if server prevents it.

Manually updating package.json

Directly editing the package.json to specify the desired versions and then installing the updates.

  1. Edit the package.json, replacing the version numbers with “*” or the latest version available.
  2. Run npm install to install the specified versions.

Example:

// In package.json before edit:
// "dependencies": {
//     "express": "^4.17.1",
//     ...
// }

// Edited package.json
// "dependencies": {
//     "express": "*",
//     ...
// }
npm install

Pros: Complete control over what versions are installed. Cons: More time-consuming and error-prone compared to other methods.

To Recap

In summary, there are multiple approaches to upgrading npm packages to their newest versions. Using npm-check-updates simplifies the process dramatically but requires additional installation. Native npm commands like npm outdated and npm update offer a straightforward solution without extra dependencies but may not update to the latest versions across major releases. Manually updating the package.json allows you to specify the exact versions but could be tedious and risky. Ultimately, the choice depends on the project requirements and the developer’s preference.