How to Install a Specific Version of an NPM Package

Avatar

By squashlabs, Last Updated: October 1, 2023

How to Install a Specific Version of an NPM Package

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.

You May Also Like

How to Differentiate Between Tilde and Caret in Package.json

Distinguishing between the tilde (~) and caret (^) symbols in the package.json file is essential for Node.js developers. This article provides a simple guide to... read more

How to Fix “nvm command not found” with Node Version Manager

A guide on fixing the "nvm command not found" issue while installing Node Version Manager. Learn how to update your shell configuration, reinstall Node Version Manager,... read more

How to Uninstall npm Modules in Node.js

Uninstalling npm modules in a Node.js project can be done using two methods: the npm uninstall command or by manually removing the module. This article provides a... read more

How to Fix the “ECONNRESET error” in Node.js

Resolving the ECONNRESET error in Node.js applications can be a challenging task. This article provides a guide on how to fix this error, focusing on common causes and... read more

How to Switch to an Older Version of Node.js

A guide on how to change to an older version of Node.js. This article provides step-by-step instructions on checking, choosing, and installing the desired Node.js... read more

How to Implement Sleep in Node.js

Node.js is a powerful runtime environment for JavaScript that allows developers to build scalable and high-performance applications. One common requirement in Node.js... read more