How to install a previous exact version of a NPM package?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In the world of software development, managing dependencies efficiently is pivotal. NPM (Node Package Manager) is the default package manager for the JavaScript runtime environment Node.js. It is widely used for sharing and transcending packages of reusable code. There may be occasions where developers need to install an exact, previous version of an NPM package. This can be essential for reasons like compatibility, testing against multiple versions, or simply reverting to a more stable release. Here, we will explore the steps to install a specific version of a NPM package and discuss best practices around version management.
Understanding Semantic Versioning
Before diving into the process of installing a specific package version, it's important to understand semantic versioning (SemVer) which NPM uses. Semantic versioning is a system of version numbering to signify different releases of software. It follows the pattern of MAJOR.MINOR.PATCH:
- MAJOR version changes when there are incompatible API changes,
- MINOR version changes when functionality is added in a backwards-compatible manner, and
- PATCH version when backwards-compatible bug fixes are introduced.
Installing a Specific Version of a Package
To install a specific version of an NPM package, follow these steps:
- Identify the Package Name and Desired Version: First, figure out the name of the package and the version number you want to install. This information can usually be found on the NPM package's page on npmjs.com.
- Use the NPM Install Command: Use the command
npm install <package-name>@<version>to install the specific version. For example, to install version 1.0.4 of lodash, you would run:
- Save the Version to
package.json: If maintaining this version in your project is important, use the--saveor--save-exactflags to update yourpackage.jsonfile. The--save-exactflag will save the exact version without the caret (^) or tilde (~) prefix, ensuring the same version is always installed during future installations.
- Verify Installation: Ensure that the correct version was installed by checking your
package.jsonfile or running:
Best Practices for Managing Dependencies
- Check for Updates Regularly: Keep your dependencies up-to-date to benefit from the latest features, performance improvements, and bug fixes.
- Understand Major Changes: Before updating to a new major version, review the release notes to understand any breaking changes.
- Use
package-lock.json: Ensure that you commit thepackage-lock.jsonoryarn.lockfile to maintain consistent installations across different environments. - Security Updates: Apply security patches and minor updates as soon as possible to minimize vulnerabilities.
Table Summarizing Key Points for Installing a Specific Version
| Step | Command | Description |
| Identify Version | Search on npmjs.com | Find the exact version number of the package required. |
| Install Specific Version | npm install <package-name>@<version> | Install the specific version of the NPM package. |
Save to package.json | npm install <package-name>@<version> --save-exact | Ensure that the exact version is locked in your project dependencies. |
| Verify Installation | npm list <package-name> | Confirm the correct version is installed. |
Conclusion
Installing a specific, previous version of an NPM package can be crucial for various reasons in a development cycle. By following the outlined steps and adhering to best practices in version management, developers can effectively manage their application dependencies, ensuring stability, compatibility, and security throughout their project's lifecycle.

