When using npm for package management, it is sometimes necessary to revert updates due to unstable or incompatible versions. Here are two methods to revert npm updates.
Method 1: Reinstall with a Specific Version
If you know the previous version number, you can directly specify the version for installation. For example, to revert the express package from its current version to 4.17.0, run the following command in the command line:
bashnpm install express@4.17.0
This command reinstalls the express package to version 4.17.0, effectively reverting the update.
Method 2: Using npm install with package-lock.json
If issues arise after running npm update and you lack the specific version number, utilize the package-lock.json file. This file records the exact version numbers of each npm package at installation time, ensuring consistency of project dependencies.
First, check the version information for the package in the package-lock.json file, then update the corresponding dependency in the package.json file to match the version recorded in package-lock.json. Finally, run the following command in the project root directory:
bashnpm install
This command reinstalls the dependency packages based on the specified versions in package-lock.json.
Example:
Suppose you accidentally updated the lodash package from 4.17.15 to 4.17.20 in a project, and this new version is incompatible. After checking package-lock.json, you find the previous version was 4.17.15. Next, change the lodash version in package.json to 4.17.15 and run npm install to revert to the previous version.
Summary
Reverting npm package updates primarily relies on reinstalling a specific version or reverting using the package-lock.json file. In project development, it is recommended to frequently commit the package-lock.json file to the version control system to quickly revert to a stable version in such cases.