Programs

How to Install Specific Version of NPM Package?

Introduction to installing specific version of NPM package

NPM (Node Package Manager) empowers 17M+ developers globally, ensuring elegant, productive, and secure JavaScript development. Complemented by advanced tools and services, NPM takes developers’ work to new heights. Just being able to install a specific version of the NPM package can help make your entire process of coding much simpler. 

In this article, we will help you equip yourself with the knowledge and techniques needed to learn how to install a specific version of node confidently.

Understanding the importance of installing specific versions

Installing specific versions of node holds significant importance as it facilitates the distribution of JavaScript code to web and mobile applications while allowing ongoing improvements without requiring users to delete and update their apps. 

Let’s understand the importance of installing specific packages – 

  • Ensure Code Sharing: Specific version installations enable seamless sharing with other NPM users worldwide, promoting collaboration and accelerating development processes.
  • Version Management: Installing specific versions allows developers to create and manage multiple versions of code, empowering them to test and evaluate different versions to determine the one that works best for their projects.
  • Performance Optimisation: By installing specific versions, developers can easily send minor updates to keep the software running at its optimal performance level, ensuring continuous improvements without disrupting user experiences.
  • Seamless Rollouts: Installing specific NPM versions facilitates the smooth rollout of improvements, allowing developers to introduce enhancements without interrupting or causing damage to the user experience.

Check out our free technology courses to get an edge over the competition.

Checking available versions of an NPM package

You can use the ‘NPM view’ command followed by the package name and the keyword ‘versions’ to check for available versions. Here’s an example:

NPM view <package-name> versions

For instance, if you want to see the available versions of the package “lodash,” you would run:

NPM view lodash versions

This command will display a list of available versions, such as ‘1.0.0‘, ‘1.1.0‘, ‘2.0.0‘, and so on. You can use this information to determine which specific version you want to install or to check if you have the latest version already installed. 

If you want to view the latest stable version, you can use

NPM view <package name> version

For example, the NPM view gulp version will provide the output 4.0.2, indicating the latest stable version of gulp. 

Check Out upGrad’s Software Development Courses to upskill yourself.

Installing specific version using NPM command line

Getting an NPM install specific version using the command line is a straightforward process. Here’s a step-by-step guide:

  1. Open your command-line interface (CLI) or terminal.
  2. Navigate to the root directory of your project or the location where you want to install the package.
  3. Use the following command to install a node install specific version:

NPM install <package-name>@<version>

  1. Replace <package-name> with the package name you want to install and <version> with your desired specific version. For example:

NPM install lodash@4.17.21

  1. This command will install version 4.17.21 of the lodash package.
  2. Wait for the installation process to complete. NPM will fetch and install the package’s specified version and dependencies.

Your specific version of an NPM package using the command line is ready now!

Installing specific version of NPM

Installing specific version using package.json file

Installing a specific version of an NPM package can also be done by specifying the version in the package.json file of your project. Here’s how you can do it:

  • Open the package.json file located in the root directory of your project.
  • Locate the “dependencies” or “devDependencies” section, depending on whether the package is a production or development dependency.
  • Add or update the entry for the package with the desired version number. For example:

“dependencies”: {

  “lodash”: “4.14.1”

}

This specifies that you want to install version 4.14.1 of the lodash package.

Installing NPM with package.json

  • Save the package.json file.
  • Open your command-line interface (CLI) or terminal.
  • Navigate to the root directory of your project.
  • Run the following command to install the dependencies:

NPM install

This command will read the package.json file and install the specified versions of the packages listed in the “dependencies” and “devDependencies” sections.

NPM install via package. json

Install Specific Version of NPM Package using Yarn CLI

The process of installing a specific version of the NPM package using Yarn CLI is very simple – 

  • Open your command-line interface (CLI) or terminal.
  • Navigate to your project’s root directory or the location where you want to install the package.
  • Use the following command to install a specific version of an NPM package with Yarn:

yarn add <package-name>@<version>

  • Replace <package-name> with the package name you aim to install and <version> with the specific version you desire. For example:

yarn add lodash@4.17.21

This command will install version 4.17.21 of the lodash package.

  • Wait for the installation process to complete. Yarn will fetch and install the package’s specified version and dependencies.

Installing NPM using Yarn CLI

Install Specific Version of NPM Package from GitHub

To install packages available at GitHub Packages and configure your project to use them, follow these steps:

  • Ensure that you have a package.json file within your project directory. If not, create one. This file will manage your project’s dependencies.
  • Authenticate GitHub Packages using the appropriate authentication method. This step ensures you have permission to access and install packages from GitHub Packages.
  • Create or edit a .NPMrc file within the same directory as the package.json file. Add the following line, replacing NAMESPACE with the account name that owns the repository where your project is hosted:

@NAMESPACE:registry=https://NPM.pkg.github.com

This configuration sets GitHub Packages as the registry for your project.

  • Add the .NPMrc file to your repository so that GitHub Packages can easily locate and use it.
  • Configure your package.json file to include the specific package you want to install. Define the full-scoped package title for GitHub Packages, such as @my-org/package, or the full name of the packages from NPMjs.com, such as @babel-core or lodash.
  • In the “dependencies” section of your package.json, add the package and define the desired version:

“dependencies”: {

  “ORGANISATION_NAME/PACKAGE_NAME”: “1.1.4”

}

  • Save the changes to your package.json file.
  • Run the following command to install the package and its dependencies:

$ NPM install

 

Learn in-depth software engineering skills with upGrad’s Executive Post Graduate Programme in Software Development – Specialisation in Full Stack Development

Using semantic versioning for package versioning

Semantic versioning is a widely adopted versioning scheme for software packages that follows a specific format: MAJOR.MINOR.PATCH. Each segment conveys a specific meaning:

MAJOR version: When you make incompatible API changes or introduce major functionality changes, you increment the MAJOR version. This indicates that the new version may not be backwards compatible with the previous versions.

MINOR version: Incrementing the MINOR version occurs when new features or functionality are added while ensuring compatibility with previous versions. This signifies that the updated version remains backwards compatible with earlier releases.

PATCH version: When you make backwards-compatible bug fixes or minor updates, you increment the PATCH version. This indicates that the new version does not introduce new features or break changes.

Semantic Versioning

Semantic versioning helps developers and users of a package understand the significance of version updates at a glance. 

When installing packages, you can specify the desired level of version flexibility using semantic versioning operators:

  • ^ (caret)
  • ~ (tilde)
  • = (equal)

You have the flexibility to specify which types of updates your package can accept from its dependencies by using semantic versioning in your package.json file.

To indicate the acceptable version ranges, you can implement the following syntax:

  • For patch releases: Use 1.0 or 1.0.x or ~1.0.4 to allow updates up to version 1.0.4.
  • For minor releases: Use 1 or 1.x or ^1.0.4 to allow updates within the 1.x.x range, but not to version 2.0.0.
  • For major releases: Use * or x to allow any major version updates.

Here’s your chance to become a pro software developer with Full Stack Software Development Bootcamp by upGrad!

In-Demand Software Development Skills

Best practices for installing specific versions of NPM packages

  • Checking NPM version:

Use npm –version to check the version of the NPM CLI.

Use npm list –depth=0 to get detailed version information of NPM, current package, Node.js, and other direct dependencies.

  • Getting help:

Use npm help <command> to access the built-in help functionality of NPM.

  • Starting new projects with NPM init:

Use npm init –yes to quickly create a package.json file with default values.

Set default values for the author name and email using npm config set init.author.name and npm config set init.author.email commands.

  • Finding NPM packages:

Utilise websites like NPMs.io to explore and evaluate NPM packages based on quality, popularity, and maintenance metrics.

  • Saving dependencies:

Install and save a package using npm install <package-name> –save.

Configure NPM to save dependencies with different prefixes or exact versions using npm config set save-prefix and npm config set save-exact.

Explore our Popular Software Engineering Courses

Troubleshooting common issues during installation

  1. Random Errors: Unidentifiable issues can be initially resolved by running the npm cache clean and then trying the installation again. Use the – verbose option if you need additional details about the error.
  2. No Compatible Version Found: If you encounter this error, you have an outdated version of NPM. Update to the latest stable version of NPM.
  3. Permissions Errors: Refer to resources on how to download and install Node.js and resolve EACCES permissions errors when installing packages globally. 
  4. NPM Login Errors: If npm login fails, ensure that your email address on npmjs.com matches the email address you provide during login. 
  5. No Space: If you encounter an ENOSPC error, you attempt to install something on a drive with insufficient space or lack of write permissions. To resolve this, you can free up disk space or configure the tmp folder to a location with more available space by using the command npm config set tmp /path/to/big/drive/tmp.

Explore Our Software Development Free Courses

Conclusion

Installing a specific version of an NPM package can be a valuable skill for developers. Whether you need to maintain compatibility with a certain package version or troubleshoot issues caused by updates, NPM provides a straightforward solution. This flexibility empowers developers to have greater control over their project dependencies and ensures a smooth development experience. So, the next time you need a specific version of an NPM package, remember that NPM has got you covered with its version-specific installation capabilities!

With upGrad’s Master of Science in Computer Science, students can learn all the in-demand skills and ascend in their Software Development careers. With AI-powered, immersive learning experiences, you are just a click away from emerging as a leading developer!

Enrol now to become a pro in software engineering!

Can I install multiple versions of an NPM package?

No, NPM only allows one package version to be installed globally. However, you can install different versions for different projects by specifying the version in each package.json file.

How can I update a specific version of an NPM package?

How can I install a specific version of an NPM package locally for a specific project?

Want to share this article?

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Software Engineering Courses

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks