To install a specific version of an NPM package in Node.js, you can use the npm install
command followed by the package name and the desired version number. Here are the step-by-step instructions:
Step 1: Check the Available Versions
Before installing a specific version of an NPM package, you should check the available versions to ensure that the version you want is actually available. You can do this by using the npm view
command followed by the package name. For example, to check the available versions of the lodash
package, you can run the following command:
npm view lodash versions
This will display a list of all the available versions of the lodash
package.
Related Article: How To Update Node.Js
Step 2: Install a Specific Version
Once you have identified the version you want to install, you can use the npm install
command followed by the package name and the desired version number. For example, to install version 4.17.21 of the lodash
package, you can run the following command:
npm install lodash@4.17.21
This will install the specified version of the lodash
package in your project.
Step 3: Update the Package.json (Optional)
If you want to specify the version of the package in your package.json
file, you can manually update the dependency section with the desired version number. For example, if you want to use version 4.17.21 of the lodash
package, you can add the following line to your package.json
file:
"dependencies": { "lodash": "4.17.21" }
After updating the package.json
file, you can run npm install
to install the specified version and update your project’s dependencies.
Step 4: Use Semver Ranges (Optional)
In addition to installing a specific version, you can also use semver ranges to specify a range of compatible versions. Semver ranges allow you to automatically install the latest version within a specific range when running npm install
. For example, if you want to install any version within the 4.x.x range of the lodash
package, you can run the following command:
npm install lodash@^4.0.0
This will install the latest version within the specified range.
Related Article: How To Check Node.Js Version On Command Line
Step 5: Best Practices
When installing specific versions of NPM packages, it is recommended to follow these best practices:
– Regularly check for updates to the package you are using to ensure that you are using the latest version with bug fixes and security patches.
– Use semver ranges to specify a range of compatible versions instead of locking yourself to a specific version. This allows you to automatically receive updates within the specified range.
– Update your package dependencies regularly to keep your project up to date with the latest versions of the packages you are using. This can be done by running npm outdated
to check for outdated dependencies and npm update
to update them.
Alternative Method: Using Yarn
If you are using Yarn as your package manager, you can use the yarn add
command to install a specific version of an NPM package. The syntax is similar to npm install
, where you specify the package name followed by the version number. For example, to install version 4.17.21 of the lodash
package using Yarn, you can run the following command:
yarn add lodash@4.17.21
Yarn will download and install the specified version of the package just like npm.