NPM (Node Package Manager) is the default package manager for installing and managing packages in Node.js. While installing a package, we may require a certain version of that package to suit its compatibility with the remaining packages of our program. There may occur many scenarios where we would want a previous specific version of an NPM package. In this article, we’ll look at some ways in which we can install a package with the exact version we desire. Let’s first look at how NPM package versions work.
How do NPM Package versions actually work?
NPM packages have a specific standard that is used for versioning packages, which is called Semantic Versioning, or SemVer in short. When we install a package without specifying a version, NPM installs the latest version available in the registry, by default. A version number consists of 3 segments each separated by dots. These segments are known as Major, Minor, and Patch.
Version numbers are typically of the form Major.Minor.Patch, where:
- Major represents significant updates in the package, like changes in core functionality.
- Minor represent minor changes without change in functionality, like the addition of new features.
- Patch represents changes like bug fixes with no addition of new features or changes in functionality.
Keeping the Semantic Versioning system in mind we can better install the most apt versions of a package for our projects.
Installing the Specific Version of NPM Package
Now that we know how versioning works in NPM, we would have to identify what version of a package we’d like to install. This can be done by going through the official documentation of the package and looking at the released updates in the registry.
After we’ve identified our package it’s time to install them into our project. Here, we’ll look at two ways in which we can install the exact previous version we want:
- Using the @ symbol in the npm install command
- Modifying the version in the package.json file
Let’s look at both of these methods with examples.
Using the @ symbol in the npm install command
The most common method to install a package would be to use the npm install command. Keep in mind that this command should be used after you have navigated to the root directory of your project. The command used to download an exact version of a package is given below.
npm install <package-name>@<version>
Here, we’ll install an exact previous version of react (17.0.1) using npm install using the command:
npm install [email protected]
Here, we have navigated to the root directory of our project, article before using the npm install command.
Modifying the version in the package.json file
Another not so straightforward, yet simple method of installing an exact version of a package is to install the package without specifying the version and then manually modify it to the version you want from the package.json file of our project. Let us see with an example how this can be done.
Here, React has been installed with its latest version (18.2.0) as we have not specified a version while using npm install.
We will now manually modify this version to 17.0.1 in the package.json file.
After updating the version of a package in package.json we have to run the below command to make changes.
The version can be checked using one of the methods given below.
Checking the Version of an Installed NPM Package
To verify whether the change in version has been reflected, we can check all the packages installed in our package.json file using the below command.
If we only want to check the version installed of a specific package, we use the command:
npm list <package-name>
We can also check the package.json file directly to see what versions of our packages are installed
Installing an exact previous version of a package can be essential for making sure that all packages in a project are compatible. To identify what package suits our needs best it is essential to know how Semantic Versioning works in npm. More than installing it is crucial to know what package version we need, this will help us install better using the methods we’ve learned in this article. With this knowledge, you will now be able to manage your Node.js dependencies efficiently.