How to use the command npm-check (with examples)
The npm-check command is a useful tool for checking and managing npm package dependencies. It allows developers to easily identify outdated, incorrect, and unused dependencies in their project and provides options for updating packages interactively or automatically. The command can be run from the command line and provides detailed reports on the status of the dependencies.
Use case 1: Display a report of outdated, incorrect, and unused dependencies
Code:
npm-check
Motivation: By running the npm-check command without any additional arguments, you can quickly generate a report that displays information about all the outdated, incorrect, and unused dependencies in your project. This report can help you identify potential vulnerabilities or performance issues associated with the outdated dependencies or dependencies that are no longer needed.
Explanation:
The npm-check
command is used without any arguments to initiate the check for outdated, incorrect, and unused dependencies. It scans the project’s package.json file and compares the listed dependencies with the latest versions available in the npm registry.
Example output:
# npm-check Report
Outdated dependencies:
Package Current Wanted Latest Location
--------------------- -------- ------- ------- --------
react 16.8.0 16.8.0 17.0.2 package.json
react-dom 16.8.0 16.8.0 17.0.2 package.json
Incorrect versions:
Package Current Expected Location
--------------------- -------- --------- ---------
express 4.17.1 >=4.16.1 package.json
Unused dependencies:
Package Location
--------------------- -----------
redux package.json
In this example, the report identifies two outdated dependencies (react and react-dom), one incorrect version (express), and one unused dependency (redux).
Use case 2: Interactively update out-of-date packages
Code:
npm-check --update
Motivation:
When you want to update specific out-of-date packages manually, you can use the --update
option with the npm-check command. This will present you with an interactive menu where you can select the packages that you want to update.
Explanation:
By using the --update
option, the npm-check command allows you to interactively update out-of-date packages. When this option is specified, a menu is displayed listing all the out-of-date packages detected during the check. You can select individual packages or choose to update all the packages at once.
Example output:
Interactive update:
? Choose which packages to update (Press <space> to select, <a> to toggle all, <i> to invert selection)
❯◯ react (16.8.0) → 17.0.2
◯ react-dom (16.8.0) → 17.0.2
In this example, the command prompts you to select the packages that you want to update interactively. You can use the spacebar to toggle the selection, and then press Enter to proceed with the update.
Use case 3: Update everything without prompting
Code:
npm-check --update-all
Motivation:
When you want to update all the out-of-date packages without manually selecting them, you can use the --update-all
option with the npm-check command. This allows you to update all the packages in one go, without having to go through the interactive menu.
Explanation:
The --update-all
option, when used with the npm-check command, updates all the out-of-date packages without displaying an interactive menu. It automatically updates all packages to their latest versions.
Example output:
All packages were successfully updated.
Outdated Packages:
Package Updated To
--------------------- ----------
react 17.0.2
react-dom 17.0.2
In this example, the command automatically updates all the out-of-date packages to their latest versions without requiring any manual intervention.
Use case 4: Don’t check for unused packages
Code:
npm-check --skip-unused
Motivation:
In some cases, you may not be interested in checking for unused packages, as they may not pose any significant issues to your project. By using the --skip-unused
option with the npm-check command, you can exclude the check for unused packages, making the process faster.
Explanation:
The --skip-unused
option, when used with the npm-check command, skips the check for unused packages. It only focuses on identifying and reporting outdated and incorrect dependencies.
Example output:
# npm-check Report
Outdated dependencies:
Package Current Wanted Latest Location
--------------------- -------- ------- ------- --------
react 16.8.0 16.8.0 17.0.2 package.json
react-dom 16.8.0 16.8.0 17.0.2 package.json
Incorrect versions:
Package Current Expected Location
--------------------- -------- --------- ---------
express 4.17.1 >=4.16.1 package.json
In this example, the report excludes the section for unused dependencies and only includes information about outdated and incorrect dependencies. This can be useful when you want to focus solely on updating outdated and incorrect dependencies.