iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🔖

How to Upgrade Packages in package.json (Using npm-check-updates and outdated)

に公開

Introduction

I would like to introduce a tool called npm-check-updates, which is very useful for updating the versions of packages listed in your package.json.

Conclusion

There are two main ways to update the packages listed in your package.json:

  • Method 1: Using the npm outdated command
    • Execute the npm outdated command, check the update information, and manually update each package one by one.
  • Method 2: Using the npm-check-updates package ← Recommended!
    • Use the npm-check-updates package to check for updates and update all packages at once.

Considering the effort involved, npm-check-updates is much easier.

Isn't 'npm update' enough for version upgrades?

It is true that running the npm update or npm update <package> command will install the latest versions within the dependency version range specified in your package.json and update package-lock.json and node_modules.

However, since it only updates to the latest version within the range specified in your package.json's dependencies or devDependencies, if the version listed in package.json is low, it will not update to the actual latest version available in the world.

Example: If ^3.2.1 is listed, npm update will not notice even if 4.0.0 has been released.

(Honestly, apart from upgrading globally installed packages, I'm not really sure in what cases npm update is necessary. I generally believe npm install is sufficient.)

Method 1: Using the npm outdated command

Behavior of npm outdated

There is a command called npm outdated that allows you to check whether a newer version exists for the installed packages.

Running npm outdated will tell you the update information for the packages listed in package.json.

# Intentionally install an older version of cowsay
$ npm install cowsay@1.2.0
+ cowsay@1.2.0

# Check package.json
$ cat package.json
{
  (omitted)
  "dependencies": {
    "cowsay": "^1.2.0"
  },
  (omitted)
}

# Running npm outdated notifies that an update from 1.2.0 to 1.4.0 is available
$ npm outdated            
Package  Current  Wanted  Latest  Location
cowsay     1.2.0   1.4.0   1.4.0  typescript-node-base

However, npm outdated only informs you that a newer version exists; it does not perform the version upgrade itself.

Upgrade Procedure

If you want to update the version information of a package listed in package.json, you must follow these steps:

  1. Use npm outdated to check if a new version has been released.
  2. If a new version has been released, uninstall the relevant package.
  3. Reinstall the package using npm install <package>.

However, with this procedure, if you are managing many packages, it is difficult to reinstall each package individually for every package that has an update available in step 2.

Method 2: Using npm-check-updates

If you use the npm-check-updates package, you can perform updates with just the following steps:

  1. Execute the ncu command (to check for update information).
  2. Execute the ncu -u command (to update package.json).
  3. Execute the npm install command (to install packages based on the updated package.json).

An execution example is shown below. We will update the old cowsay package along with package.json.

# Current status of package.json
$ cat package.json
{
  (omitted)
  "dependencies": {
    "cowsay": "^1.2.0"
  },
  (omitted)
}

# Check for upgradeable packages with ncu (Most articles show global installation, but since it is not used frequently, I think executing it with npx is fine)
$ npx -p npm-check-updates  -c "ncu"
npx: 285 packages installed in 6.971 seconds.
Checking package.json
[====================] 2/2 100%

 cowsay  ^1.2.0  ^1.4.0   

Run ncu -u to upgrade package.json


# Executing ncu -u updates package.json
$ npx -p npm-check-updates  -c "ncu -u"
npx: 285 packages installed in 6.971 seconds.
Checking package.json
[====================] 2/2 100%

 cowsay  ^1.2.0  ^1.4.0   

Run npm install to install new versions.


# Confirm that package.json has been updated
$ cat package.json
{
  (omitted)
  "dependencies": {
    "cowsay": "^1.4.0"
  },
  (omitted)
}


# Execute npm install based on the updated package.json
$ npm install

Although the sample only contains one package for clarity, ncu -u will update all packages at once even if there are multiple.

Also, it seems there are various things you can do, such as specifying only certain packages, specifying multiple packages with regular expressions, or specifying version upgrades by minor version or higher, or patch version or higher (please refer to the official reference for details).

[Reference]
Checking for updates and upgrading npm-installed packages (npm-check-updates)

[Reference] depcheck

There is also a tool called depcheck that tells you about unused packages.
I recommend using this tool to delete unnecessary packages before considering updates.

To use it, simply execute the command npx depcheck.

Example of use:

# Before deleting unused packages
$ npx depcheck
npx: 108 packages installed in 6.778 seconds.
Unused dependencies
* bootstrap
* jquery
* path
Unused devDependencies
* babel-eslint
* casperjs

# After deleting unused packages
$ npx depcheck
npx: 108 packages installed in 4.581 seconds.
No depcheck issue

[Reference]
I explain npx in the following article:
How to run locally installed npm packages via CLI (npm-scripts, npx)

Official References

GitHubで編集を提案

Discussion