How to Install a Specific Version of an NPM Package

Avatar

By squashlabs, Last Updated: Oct. 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 Set the Default Node Version Using Nvm

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: Integrating HTMX with Javascript Frameworks

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

Advanced Node.js: Event Loop, Async, Buffer, Stream & More

Node.js is a powerful platform for building scalable and web applications. In this article, we will explore advanced features of Node.js such as the … read more

How To Downgrade To A Previous Node Version In Nodejs

Guide on downgrading to an earlier Node version in Nodejs. Learn how to use Node Version Manager (NVM) and manually install older versions. Additiona… read more

How to Run 100 Queries Simultaneously in Nodejs & PostgreSQL

Learn how to execute 100 queries simultaneously in Node.js with PostgreSQL. Understand how PostgreSQL handles executing multiple queries at once and … read more

How to Use Force and Legacy Peer Deps in Npm

A simple guide on using force and legacy peer deps features in Npm within Node.js context. Learn how to utilize the force flag and the legacy peer de… read more

Big Data Processing with Node.js and Apache Kafka

Handling large datasets and processing real-time data are critical challenges in today's data-driven world. In this article, we delve into the power … 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… read more

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 simp… read more

How to Check the Version of an Installed npm Package

Checking the version of an installed npm package in Node.js is a fundamental task for developers. This article provides two methods for achieving thi… read more

Fix The Engine Node Is Incompatible With This Module Nodejs

A simple guide for resolving the 'engine node' compatibility issue in Nodejs module. This article provides two solutions: updating Node.js and using … read more

Building a Language Learning Game with GraphQL & Nodejs

This tutorial teaches readers how to create a language learning game using GraphQL, Node.js, React.js, MongoDB, and Docker. The article covers topics… read more