Learn How to Instantly Install a Specific Version of an NPM Package!

By Pavan Vadapalli

Updated on Sep 22, 2025 | 11 min read | 22.71K+ views

Share:

Have you ever updated a npm package only to have your project break? Or maybe you need to work on an older project that depends on a particular version of a library. Knowing how to install a specific version npm package is a fundamental skill for any JavaScript developer to ensure project stability and consistency across different machines and environments. 

In this detailed blog, you will learn everything about managing package versions. We will cover all the different methods for installing packages, explore the core concepts of semantic versioning, and understand how package.json and package-lock.json work together to control your project's dependencies.  

JavaScript is crucial for scalable apps, and NPM helps manage dependencies. upGrad’s Software Engineering Courses provide training in JavaScript, system architecture, and core programming to build reliable software. Enroll today! 

How to Install a Specific Version of NPM Package: All the Methods 

There are several ways to specify which version of a package you want to install. The method you choose depends on how precise you need to be. Let's cover all the commands you can use. 

Improve your programming skills with courses on tools like NPM, covering techniques to manage dependencies, use AI, and build scalable applications, such as: 

Installing an Exact Version Number 

This is the most common and direct method. You tell npm to fetch one specific version, and it will not deviate. This is perfect for ensuring stability when you know a particular version works with your code. 

Syntax: 

Bash 
npm install <package-name>@<version-number> 
 

Example: 

To install exactly version 17.0.2 of React, you would run: 

Bash 
npm install react@17.0.2 
 

This command adds "react": "17.0.2" to your package.json, locking it to that version. 

Also Read: How to Install Dev Dependencies in npm? A Complete Guide to Dependencies & Scripts 

Installing Using Version Ranges 

Sometimes you want to allow for minor updates or bug fixes. You can do this using special characters in the version specifier. 

1. Using the Caret (^) for Minor Updates 

The caret is the default for npm install. It allows for any patch or minor updates but will not install a new major version. This is a good balance between stability and getting new features. 

  • Syntax: npm install <package-name>@^<version-number> 
  • Example: npm install lodash@^4.17.20 
  • What it means: This will install version 4.17.20 or any later version up to, but not including, 5.0.0. So, 4.18.0 would be installed, but 5.0.0 would not. 

2. Using the Tilde (~) for Patch Updates 

The tilde is more restrictive. It only allows for patch-level updates, which are typically just for bug fixes. This is a safer, more conservative approach. 

  • Syntax: npm install <package-name>@~<version-number> 
  • Example: npm install lodash@~4.17.20 
  • What it means: This will install version 4.17.20 or any later patch version up to, but not including, 4.18.0. It will install 4.17.21 but not 4.18.0. 

Also Read: Top 45+ Nodejs Project Ideas for Beginners and Professionals 

Software Development Courses to upskill

Explore Software Development Courses for Career Progression

Coverage of AWS, Microsoft Azure and GCP services

Certification8 Months

Job-Linked Program

Bootcamp36 Weeks

Installing Using Tags 

Packages often have tags, which are human-readable labels that point to a specific version. The most common tag is latest. 

  • Syntax: npm install <package-name>@<tag-name> 
  • Example: npm install react@latest 
  • What it means: This will look up which version the latest tag points to and install it. Other common tags include beta or next for pre-release versions. 

Installing from a Git Repository 

You can also install a package directly from a Git repository, like GitHub. This is useful for installing a forked version of a package or a version that has not been published to the npm registry yet. 

  • Syntax: npm install <git-remote-url> 
  • Example: npm install git+https://github.com/expressjs/express.git 
  • What it means: This command clones the repository and installs the package. You can even specify a particular branch or commit hash: npm install expressjs/express#main. 

Also Read: How to Use GitHub: A Beginner's Guide to Getting Started and Exploring Its Benefits in 2025 

Installing from a Local Folder or Tarball 

If you have the package code on your local machine, you can install it directly from the folder. This is great for local development and testing of packages you are creating yourself. 

  • Syntax: npm install <folder-path> 
  • Example: npm install ../my-local-package 
  • What it means: This creates a symbolic link to your local package folder inside node_modules. Any changes you make to the local package code will be immediately reflected in your project. 

 Quick Summary Table: 

Method 

Syntax 

Example 

What It Does 

Exact Version  npm install <package>@<version>  npm install react@17.0.2  Installs the exact version and locks it in package.json. 
Caret (^) for Minor Updates  npm install <package>@^<version>  npm install lodash@^4.17.20  Installs the specified version or any higher minor/patch version, but not the next major release. 
Tilde (~) for Patch Updates  npm install <package>@~<version>  npm install lodash@~4.17.20  Installs the specified version or only patch updates (bug fixes), but not the next minor release. 
Tag (e.g., latest, beta, next)  npm install <package>@<tag>  npm install react@latest  Installs the version linked to the tag, like latest or beta. 
Git Repository  npm install <git-url>  npm install git+https://github.com/expressjs/express.git  Installs a package directly from a Git repo, branch, or commit. 
Local Folder or Tarball  npm install <path>  npm install ../my-local-package  Installs a package from a local folder or tarball, often for development/testing. 

Enhance your career with the Professional Certificate Program in Cloud Computing and DevOps. Gain practical experience with AWS, Azure, and Google Cloud, and master key DevOps tools like Docker and Kubernetes.

Understanding Semantic Versioning (SemVer) 

To properly install a specific version npm package, you need to understand what the version numbers mean. The npm ecosystem uses a standard called Semantic Versioning, or SemVer, which gives meaning to each number in a version like 16.8.0. 

The format is always MAJOR.MINOR.PATCH

  • MAJOR (16.x.x): This number is incremented when an update contains breaking changes. A breaking change means the new version is not backward-compatible. If you upgrade to a new major version, you might need to update your own code to adapt to the changes. 
  • MINOR (16.8.x): This number is incremented when new features are added in a backward-compatible way. Your existing code should continue to work without any changes after a minor update. 
  • PATCH (16.8.0): This number is incremented for backward-compatible bug fixes. These are the safest updates, as their main purpose is to correct errors in the code. 

This system provides a clear way for package authors to communicate the impact of their changes, helping you make informed decisions about which versions to install. 

Also Read: Yarn vs NPM: Key Differences and How to Choose the Right Package Manager in 2025 

The Role of package.json and package-lock.json 

Your project's dependencies are managed by two very important files: package.json and package-lock.json. They work together to ensure your project's dependencies are both flexible and consistent. 

package.json - The Project Manifest 

This file is the main configuration file for your project. It contains important metadata, such as the project's name, version, and scripts. Its most important role in dependency management is the dependencies section. 

When you install a specific version npm package, an entry is added to this file. 

Example dependencies in package.json: 

JSON 
"dependencies": { 
 "lodash": "^4.17.21", 
 "react": "~17.0.2", 
 "moment": "2.29.4" 
} 
 

This section acts as a set of rules. It tells npm that the project is compatible with: 

  • Any version of lodash from 4.17.21 up to 5.0.0. 
  • Any patch version of react from 17.0.2 up to 17.1.0. 
  • Exactly version 2.29.4 of moment. 

package-lock.json - The Exact Recipe 

This file is automatically generated by npm the first time you install a package. It is a much more detailed file that records the exact version of every single package and sub-package that was installed in your node_modules folder. 

Why is this file so important? 

The package-lock.json file guarantees that your project is reproducible. When another developer clones your project and runs npm install, npm will read the package-lock.json file and install the exact versions listed there, completely ignoring the version ranges in package.json. This ensures that every member of the team has the exact same set of dependencies, which prevents "it works on my machine" issues. 

You should always commit your package-lock.json file to your version control system (like Git). 

Also Read: package.json vs package-lock.json: Major Differences 

How to Find Available Versions of a Package 

Before you can decide how to install a specific version of npm package, you first need to know what your options are. NPM provides easy ways to see all the published versions of a package. 

Method 1: Using the npm view Command 

You can check for available versions directly from your command line using the npm view command (or its shorthand npm v). 

To get a full list of all available versions for a package, run: 

Bash 
npm view <package-name> versions 
 

Example: 

Bash 
npm view lodash versions 
 

This command will print out an array of strings, with every version of Lodash that has ever been published. This is helpful for finding a specific older version or checking for the latest release. 

Also read: Top Front-End Developer Interview Questions for 2025 

Method 2: Using the NPM Website 

A more user-friendly way to see available versions is to use the official npm website. 

  1. Open your web browser and go to https://www.npmjs.com/. 
  2. Use the search bar to find your package (e.g., "lodash"). 
  3. On the package's page, click on the "Versions" tab. 

This page provides a clean, chronological list of all the package's versions, along with their publication dates. This can make it easier to find the version you are looking for. 

Managing and Updating Your Packages 

Once you have packages installed, you will also need to manage them over time. NPM provides commands to help with this. 

Checking for Outdated Packages 

To see which of your installed packages have newer versions available (based on the rules in your package.json), you can run: 

Bash 
npm outdated 
 

This command produces a table showing the current version, the wanted version (based on your package.json ranges), and the latest version available. This is a great way to see what updates are available for your project. 

Also Read: Full Stack Developer Salary in India 2025: Trends & Top Recruiters 

Updating Packages 

To update your packages to the newest versions allowed by your package.json ranges, you can run: 

Bash 
npm update 
 

This command will go through your dependencies, find newer versions that satisfy the caret (^) and tilde (~) ranges, and install them. It will also update your package-lock.json file to reflect these new versions. 

If you want to update a package to a new major version, npm update will not do it. You must do this manually by running the npm install command with the new version. For example: 

Bash 
# To upgrade React from version 17 to 18 
npm install react@18.0.0 

Conclusion 

With a solid grasp of these commands and concepts, you have complete control over your project's dependencies. The ability to install a specific version npm package is a crucial skill that ensures your applications are stable, consistent, and maintainable. 

Become an expert in AI-Driven Full-Stack Development with upGrad’s 8-month bootcamp. Gain certifications from Microsoft, NSDC, and upGrad, and enhance your skills with real-world projects. Show Interest now to get sponsorship for Microsoft Azure AI certification, mentorship, and career support.

Get started with dynamic web development in our React.js for Beginners course. Learn key concepts like the Virtual DOM, ES6 features, and bundling, and build your own Phone Directory app. Perfect for beginners aiming to create responsive, high-performance UIs.

Boost your career with our popular Software Engineering courses, offering hands-on training and expert guidance to turn you into a skilled software developer.

Subscribe to upGrad's Newsletter

Join thousands of learners who receive useful tips

Promise we won't spam!

Master in-demand Software Development skills like coding, system design, DevOps, and agile methodologies to excel in today’s competitive tech industry.

Stay informed with our widely-read Software Development articles, covering everything from coding techniques to the latest advancements in software engineering.

Frequently Asked Questions (FAQs)

1. What is the difference between dependencies and devDependencies?

The distinction between these two is crucial for efficient project management. dependencies are the packages your application needs to function in a production environment; for example, react for the user interface or express for the server. In contrast, devDependencies are packages only used during the development and testing process, such as jest for running tests or webpack for bundling your code. When you deploy your application, you can install only the production dependencies, resulting in a smaller and more secure final build. 

2. How do I update a package to a specific version?

You can update (or even downgrade) a package to a specific version using the exact same command you would use for a fresh installation: npm install <package-name>@<version-number>. For example, if you have version 16.8.0 of React installed and want to update to 17.0.2, you would run npm install react@17.0.2. This command will fetch the specified version, replace the old one in your node_modules folder, and update both your package.json and package-lock.json files to reflect the change. 

3. What does the -g flag do in npm install?

The -g flag, which stands for --global, installs a package in a central location on your system, separate from your individual project folders. This is intended for command-line interface (CLI) tools that you want to be able to run from any directory in your terminal. For example, you would install tools like nodemon or create-react-app globally. You should not use the -g flag for project-specific libraries like lodash or react, as these should always be installed locally within your project. 

4. How can I install a package from a GitHub repository?

You can install a package directly from a Git repository, which is useful for testing unreleased features or using a forked version of a project. The syntax is npm install <username>/<repository-name>. You can get even more specific by adding a branch, tag, or commit hash at the end, like npm install expressjs/express#main. This tells npm to clone the repository and install the package from that specific reference point. 

5. What is npm ci and how is it different from npm install?

npm ci stands for "clean install" and is a stricter, faster version of npm install intended for automated environments like build servers. Unlike npm install, which can modify your package-lock.json file based on package.json, npm ci performs a clean installation purely from the package-lock.json file. It first deletes the node_modules folder to ensure no old packages remain and will fail if the package-lock.json is missing or out of sync. 

6. What happens if the version I specify doesn't exist?

If you try to install a package version that has not been published to the npm registry, the installation will fail with an error. NPM will typically report a 404 Not Found error in the terminal, indicating that the specific version you requested could not be located. The error message will often helpfully list all the available versions of that package so you can correct your command and try again. 

7. How can I find out which version of a package is currently installed?

The npm list command is the tool for this job. To check a specific package, run npm list <package-name>. For instance, npm list lodash will show you exactly which version of Lodash is in your node_modules folder. Running npm list without specifying a package name will display a tree of all installed packages and their dependencies, which can be useful for a high-level overview. 

8. What are peer dependencies?

A peer dependency is a special type of dependency that a package expects your project to already have installed. Think of it like a plugin for a piece of software; for example, the react-router-dom package requires react to be present in your project to function. The package doesn't bundle React itself, but rather declares it as a "peer" that it needs to work with. NPM versions 7 and higher handle installing these for you automatically. 

9. Can I install multiple versions of the same package?

While you cannot list multiple versions of the same package directly in your package.json's dependencies, your node_modules folder can absolutely contain them. This happens when your different dependencies themselves rely on different versions of another package. For example, if package-A requires lodash@3.0.0 and package-B requires lodash@4.0.0, npm is smart enough to install both versions in your node_modules folder to satisfy each dependency's requirements. 

10. How do I uninstall a package?

To properly remove a package from your project, you should use the npm uninstall <package-name> command (or its shorthand, npm un). This command does more than just delete files; it removes the package from your node_modules folder, and it also automatically updates your package.json and package-lock.json files to remove the dependency entry. This ensures your project configuration stays clean and accurate. 

11. What is the node_modules folder?

The node_modules folder is the heart of your project's dependencies. It is a local directory where npm downloads and stores the actual code for every package your project relies on. This includes not just the packages you explicitly installed, but all of their dependencies as well, creating a deep dependency tree. Because this folder can become very large, it is standard practice to add it to your .gitignore file to avoid committing it to version control. 

12. Should I commit package-lock.json to Git?

Yes, you should always commit your package-lock.json file to your version control system (like Git). This file acts as a definitive record of the exact versions of every package that was installed. By committing it, you ensure that every other developer on your team, as well as your automated deployment pipeline, will install the exact same set of packages, guaranteeing reproducible builds and preventing "it works on my machine" issues. 

13. What is the -D or --save-dev flag for?

This flag tells npm to install a package as a devDependency, which means it is only needed for the development process. When you run a command like npm install prettier -D, the prettier package is added to the devDependencies section of your package.json. The advantage is that when you deploy your application for production, you can run npm install --production, and npm will skip installing these development packages, resulting in a smaller, faster, and more secure installation. 

14. How can I see a package's dependencies before installing it?

You can inspect a package's dependencies without installing it by using the npm view command. Running npm view <package-name> dependencies will query the npm registry and return a list of all the packages that your chosen package depends on to function. This is a useful way to gauge the complexity of a package and see how many other libraries it might add to your project. 

15. What does the npm outdated command do?

The npm outdated command is a very useful tool for managing updates. It scans your package.json file and compares the currently installed versions of your packages against the latest versions available in the npm registry. It then produces a report showing which packages have newer versions available, helping you identify potential updates for bug fixes, new features, or security patches. 

16. Can I install a package without saving it to package.json?

Yes, you can use the --no-save flag to prevent npm from modifying your package.json or package-lock.json files. For example, npm install lodash --no-save will download Lodash to your node_modules folder but will not record it as a dependency. This is primarily useful for temporarily trying out a package to see if you like it before deciding to officially add it to your project. 

17. What is npm?

NPM, or the Node Package Manager, is the central component of the modern JavaScript ecosystem. It consists of two main parts: the npm registry, which is a massive online database hosting millions of open-source JavaScript packages, and the npm CLI (Command-Line Interface), which is the tool you run in your terminal to interact with the registry to install, update, and manage those packages for your projects. 

18. How do I update npm itself?

The npm command-line tool is a package itself, so you can update it using npm. To get the latest version of the npm CLI, you should run the command npm install -g npm@latest. The -g flag ensures you are updating the global version of npm on your system. It is a good practice to do this from time to time to benefit from the latest features, performance improvements, and security updates to the tool. 

19. What is a "breaking change"?

A "breaking change" is any modification to a package's code that is not backward-compatible with its previous versions. For example, if a function is renamed, a parameter is removed, or its behavior is fundamentally altered, your existing code that uses that function will likely fail or "break." According to Semantic Versioning, any release with a breaking change must increment its MAJOR version number (e.g., from 2.5.0 to 3.0.0). 

20. What is a dependency tree?

A dependency tree is the complete, hierarchical structure of all the packages required by your project. The trunk of the tree is your project itself. The main branches are the direct dependencies listed in your package.json. Each of those branches can have smaller branches, which are their own dependencies, and this continues until all requirements are met. The package-lock.json file is essentially a detailed, serialized map of this entire tree. 

Pavan Vadapalli

900 articles published

Pavan Vadapalli is the Director of Engineering , bringing over 18 years of experience in software engineering, technology leadership, and startup innovation. Holding a B.Tech and an MBA from the India...

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

India’s #1 Tech University

Executive PG Certification in AI-Powered Full Stack Development

77%

seats filled

View Program

Top Resources

Recommended Programs

upGrad

upGrad

AI-Driven Full-Stack Development

Job-Linked Program

Bootcamp

36 Weeks

upGrad

upGrad KnowledgeHut

Professional Certificate Program in UI/UX Design & Design Thinking

#1 Course for UI/UX Designers

Bootcamp

3 Months

IIIT Bangalore logo
new course

Executive PG Certification

9.5 Months