What is NCU?
NCU, short for npm-check-updates, is a powerful command-line tool that allows you to automatically update your package.json file with the latest versions of your project’s dependencies. It scans your package.json file, checks for newer versions of the installed packages, and generates an updated package.json file with the new dependency versions.
Before we dive into upgrading packages using NCU, let’s make sure we have it installed globally on our machine. Open your terminal and run the following command:
npm install -g npm-check-updates
Once NCU is installed, navigate to your project’s root directory using the terminal.
Open the terminal and navigate to your project’s root directory.
- Run the following command to generate an updated package.json file with the latest package versions:
NCU will scan your package.json file and display a list of available updates for your dependencies. It will also indicate the currently installed version and the latest version available for each package.
- To apply the updates and overwrite your existing package.json file with the new versions, run the following command:
5.NPM will now install the updated packages based on the modified package.json file.
Benefits of Using NCU:
Time-saving: Instead of manually checking for updates and updating each package individually, NCU automates the process, saving you valuable time.
Easy to use: NCU provides a simple command-line interface, making it accessible to developers of all skill levels.
Reliable updates: NCU only updates the versions specified in the package.json file, ensuring that your project’s dependencies remain consistent and compatible.
Upgrading Minor and Patch Versions:
Run the following command to generate an updated package.json file with the latest minor and patch versions:
ncu -u --upgradeAll --target minor
In the above command, we use the –upgradeAll flag to upgrade all packages and the –target minor flag to target only the minor version upgrades. This ensures that major versions, which may introduce breaking changes, are not updated.
Improved stability and security: By regularly updating your packages, you can benefit from bug fixes, performance improvements, and security patches released by the package maintainers.
Better collaboration: By keeping your packages up-to-date, you ensure that your team members are working with the same version of each dependency, minimizing compatibility issues.