Learn How to Instantly Install a Specific Version of an NPM Package!
Updated on Sep 22, 2025 | 11 min read | 22.71K+ views
Share:
For working professionals
For fresh graduates
More
Updated on Sep 22, 2025 | 11 min read | 22.71K+ views
Share:
Table of Contents
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!
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:
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
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.
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.
Also Read: Top 45+ Nodejs Project Ideas for Beginners and Professionals
Software Development Courses to upskill
Explore Software Development Courses for Career Progression
Packages often have tags, which are human-readable labels that point to a specific version. The most common tag is latest.
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.
Also Read: How to Use GitHub: A Beginner's Guide to Getting Started and Exploring Its Benefits in 2025
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.
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. |
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.
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
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.
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:
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
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.
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
A more user-friendly way to see available versions is to use the official npm website.
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.
Once you have packages installed, you will also need to manage them over time. NPM provides commands to help with this.
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
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
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.
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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).
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.
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
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
Top Resources