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 Run 100 Queries Simultaneously in Nodejs & PostgreSQL

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 Upgrade Node.js To The Latest Version

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 Update Node.Js

Node.js is an essential tool for many developers, and keeping it up to date is crucial for a smooth development process. In this article, you will learn why updating... read more

How To Check Node.Js Version On Command Line

Checking the Node.js version on the command line is an essential skill for any Node.js developer. In this article, you will learn various methods to quickly determine... read more

How to Use Embedded JavaScript (EJS) in Node.js

In this comprehensive tutorial, you will learn how to incorporate Embedded JavaScript (EJS) into your Node.js application. From setting up the development environment to... read more

How to Git Ignore Node Modules Folder Globally

Setting up Git to ignore node_modules folders globally can greatly simplify your development workflow. This article provides a simple guide on how to achieve this,... 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. Additional tips and best... read more