NPM: How to Install a Specific Version

Written by Diana Bocco

Content verified by Alex Ioannides

Last updated on: November 26, 2024

The Node Package Manager (npm) is the default package manager for Node.js, with over 2.1 million packages available in its registry as of September 2022. Installing a specific npm version can simplify your coding process, ensuring stability and preventing issues caused by incompatible updates. Here are three easy steps to install a specific npm version:

Step1: Identify the version: Determine the version number you need.

Step 2: Install the specific version: Run npm install [package-name]@[version-number] to install or update to specific version.

Step 3: Verify installation: Use npm list [package-name] to confirm the installed version.

Following these steps will help you manage your project’s dependencies effectively. For additional stability and performance monitoring, consider integrating a reliable uptime monitoring solution.

Install a specific npm version using the command line

  1. Run the command: 
npm install [package-name]@[version-number]

This command will install exact version of npm package you want, alongside any packages it depends on. 

  1. Example:
 npm install renovate@20.5.1 

This will install version 20.5.1 of the renovate package.

  1. Shortened version:

You can also use the shorthand:

npm i renovate@20.5.1

The above commands will install renovate locally. 

  1. Global Installation:

To install the package globally, simply add the -g flag: 

npm install -g renovate@20.5.1

This ensures the package is available system-wide, rather than just in your local project.

Install a specific npm version using package.json

  1. Open the package.json File:
    Navigate to the root directory of your project and open the package.json file.
  1. Locate the Dependencies Section:
    Depending on whether the package is for production or development, find either the “dependencies” or “devDependencies” section.
  1. Add or Update the Package Version:

Specify the desired version of the package. For example:

“dependencies”: {
  “lodash”: “4.14.1”
}

This ensures that version 4.14.1 of the lodash package is installed.

  1. Save the package.json File and open your command-line interface (CLI).
  2. Navigate to Your Project’s Root Directory and run the following command:
npm install

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

Install a specific npm version using Yarn CLI

  1. Open Your Command-Line Interface (CLI):

Navigate to your project’s root directory or the location where you want to install the package.

  1. Run the command:
yarn add <package-name>@<version>

Replace <package-name> with the name of the package and <version> with the desired version. For example:

yarn add lodash@4.17.21

This installs version 4.17.21 of the lodash package.

  1. Wait for Installation:

Yarn will fetch the specific npm version and its dependencies, completing the installation.

Install a specific npm version from GitHub

To install packages from GitHub Packages, follow these steps:

  1. Ensure package.json Exists:
    Make sure your project has a package.json file. If not, create one to manage dependencies.
  1. Authenticate GitHub Packages:
    Authenticate using the appropriate method to ensure you have permission to access the package.
  1. Configure .npmrc:
    Create or edit a .npmrc file in the root directory (where package.json is located) and add this line:
@NAMESPACE:registry=https://NPM.pkg.github.com

Replace NAMESPACE with the GitHub account or organization that owns the repository.

  1. Add the .npmrc File to Your Repository: This ensures GitHub Packages can locate and use the configuration.
  1. Update package.json: In the “dependencies” section of your package.json, specify the GitHub package and version, like so:
"dependencies": {
 "@ORGANISATION_NAME/PACKAGE_NAME": "1.1.4"
}
  1. Run the Installation:

Use the following command to install package version and its dependencies:

npm install

This process lets you install specific package versions from GitHub Packages, ensuring your project is always using the correct versions.

How to know which package versions are available

Option 1:

Search for the package on the NPM public registry to view all available versions.

Option 2:

Run the following command to check all available versions on the NPM registry: 

npm view [package-name] versions

To find the latest version of a package, use:

npm view [package-name] version 

How to install an older version of an npm

If you need to install an older version of a package—perhaps to resolve compatibility issues or bugs—you can easily do so using the @ syntax. This process is identical to installing any specific version of a package.

npm install [package-name]@[version-number]

Simply replace [version-number] with the version you need, and NPM will install the older version of the package.

How to use semantic versioning to specify package versions

Semantic Versioning (SemVer) is a system that NPM uses to version packages. Each version consists of three numbers: major.minor.patch (e.g., 24.42.1), representing major changes, minor updates, and patch fixes, respectively.

NPM allows you to use SemVer symbols to specify which version range to install:

  • Caret (^): Installs the latest minor version.
    For example, the command:
npm install [package-name]^20.0.0

This fetches the highest minor version under 20.x.x, such as 20.25.9.

  • Tilde (~): Installs the latest patch version.

For example:

npm install [package-name]~23.14.0

This fetches the latest patch under 23.14.x, such as 23.14.8.

These symbols help keep your packages up-to-date with minor improvements and security patches while maintaining compatibility.

Why it’s important to install specific versions

Let us look at a few important reasons why you should install specific versions of Node and NPM packages:

  • Code Sharing: Specific versions enable easy sharing and collaboration among developers worldwide, speeding up the development process.
  • Testing: Installing specific npm versions allows developers to manage and test multiple code versions, helping identify the best fit for their projects.
  • Performance Optimization: By controlling versions, developers can implement minor updates to maintain optimal software performance.
  • Seamless Rollouts: Installing specific versions ensures smooth rollouts of improvements, allowing enhancements without disrupting the user experience.
  • Stability: Newer versions may introduce bugs or breaking changes. Using a known stable version can prevent unexpected issues.

How to check available versions of an npm package

To view all available versions of an NPM package, use command:

npm view <package-name> versions

For example, to see the available versions of the express package, run:

npm view express versions

This will list all versions, such as 4.16.4, 4.17.1, 5.0.0, etc., helping you decide which version to install or verify if you have the latest one.

To view the latest stable version of a package, use:

npm view <package-name> version

For example:

npm view react version

This command will output the latest stable version, such as 18.2.0 for react.

How to use semantic versioning for package versioning

Semantic Versioning (SemVer) is a structured versioning scheme that follows the format: MAJOR.MINOR.PATCH. Each segment of the version number conveys specific information:

  • MAJOR version: Increment this when you make incompatible API changes or introduce major new features that break backward compatibility. For example, moving from 1.0.0 to 2.0.0 indicates significant changes that might not be compatible with previous versions.
  • MINOR version: Increment this when you add new features or functionality in a backward-compatible manner. For example, changing from 1.0.0 to 1.1.0 means new features have been added without breaking compatibility.
  • PATCH version: Increment this for backwards-compatible bug fixes or minor improvements. For example, updating from 1.0.0 to 1.0.1 signifies that only minor changes or bug fixes were made.

Specifying Version Flexibility

When specifying package versions in your package.json file, you can use semantic versioning operators to define the acceptable range of versions:

  • Caret (^): Allows updates that do not change the left-most non-zero digit.
    For example, ^1.0.4 allows updates to any 1.x.x version, but not to 2.0.0.
  • Tilde (~): Allows updates that do not change the left-most digit specified.
    For example, ~1.0.4 allows updates up to 1.0.x, but not to 1.1.0.
  • Equal (=): Specifies an exact version.
    For example, =1.0.4 only allows that exact version.
  • Asterisk (*): Allows any version.
    For example, * allows updates to any version, which can be useful for testing or in environments where version constraints are not critical.

Examples of Version Ranges

  • 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 2.0.0.
  • For major releases: Use * or x to allow any major version updates, such as ^1.0.4 allowing updates within the 1.x.x range, or * allowing any major version.

Semantic versioning helps ensure clarity and compatibility when managing dependencies, allowing you to understand the impact of version updates at a glance.

Best practices for installing specific npm versions

To keep your project stable and secure, it is important to efficiently manage npm packages and their versions. Following these best practices will help everything run smoothly and stay current. Here are some simple tips to help you install specific version of npm effectively.

  1. Getting help

Use npm help <command> to get information and usage details for any NPM command.

  1. Starting new projects with NPM init

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

  1. Finding NPM packages 

Use websites like npmjs.com to explore, evaluate, and compare NPM packages based on quality, popularity, and maintenance metrics.

  1. Using Package Lock Files

Use package-lock.json to lock dependencies to specific versions. This ensures that all team members and environments use the same versions, avoiding inconsistencies.

  1. Regularly check for updates 

Regularly check for outdated packages with npm outdated to stay current with updates and patches.

  1. Use versions wisely

Specify version ranges in package.json to control the level of flexibility for updates (e.g., ^ for minor updates, ~ for patch updates).

  1. Security practices

Run npm audit to check for security vulnerabilities in your dependencies and npm audit fix to automatically fix issues.

  1. Keep Documentation Updated 

Maintain clear documentation of the dependencies and their versions used in your project, including any specific installation instructions or version constraints.

Troubleshooting common issues

When working with NPM, you might encounter various issues that can disrupt your development process. Here are some common problems and their solutions to help you troubleshoot effectively.

1. Random Errors:

Error message: Various unidentifiable errors

Solution: Run npm cache clean and try the installation again. If you are still having errors with the npm install, use the –verbose option for more detailed error information.

2. No Compatible Version Found:

Error message: “No compatible version found”

Solution: Update to the latest stable version of NPM, as this error often indicates an outdated NPM version.

3. Permissions Errors:

Error message: “EACCES: permission denied”

Solution: Refer to resources on how to download and install Node.js and resolve EACCES permissions errors when installing packages globally. 

4. NPM Login Errors:

Error message: “Login failed”

Solution: Ensure that the email address used during login matches the one registered on npmjs.com.

5. Disk Space Issues:

Error message: “npm ERR! Error: ENOSPC, write”

Solution: Free up disk space or set NPM’s tmp folder to a location with more space using: npm config set tmp /path/to/big/drive/tmp.

6. Git Errors:

Error message: “npm ERR! not found: git” or “ENOGIT”

Solution: Install Git if it’s missing, or ensure that your Git information is added to your NPM profile. You can update your profile information from the command line or the npm website. For more details, see “Managing your profile settings.”

7. Dependency Conflicts:

Error message: “Peer dependency conflicts”

Solution: Use npm install –legacy-peer-deps to bypass peer dependency issues or update the conflicting packages to compatible versions.

Summary

Installing a specific version of a package with NPM is a simple way to ensure compatibility and stability in your projects. By using commands like npm install [package]@[version], you can manage dependencies efficiently, whether rolling back to a previous version or installing a stable release. This practice helps avoid conflicts and supports smooth development workflows. 

Once your dependencies are correctly managed, using uptime monitoring tools like UptimeRobot ensures your application is always live and responsive. This helps detect any unexpected downtime that might arise from changes in package versions. 

For complete reliability, integrate UptimeRobot’s free uptime monitoring to keep your applications running smoothly—get started today!

Diana Bocco

Written by

Diana Bocco

N/A |

Diana Bocco combines her expertise to offer in-depth perspectives on uptime monitoring and website performance. Her articles are grounded in practical experience and a deep understanding of how robust monitoring can drive business success online. Diana's commitment to explaining complex technical concepts in accessible language has made her a favorite among readers seeking reliable uptime solutions.

Expert on: None

🎖️

Our content is peer-reviewed by our expert team to maximize accuracy and prevent miss-information.

Alex Ioannides

Content verified by

Alex Ioannides

Head of DevOps |

Prior to his tenure at itrinity, Alex founded FocusNet Group and served as its CTO. The company specializes in providing managed web hosting services for a wide spectrum of high-traffic websites and applications. One of Alex's notable contributions to the open-source community is his involvement as an early founder of HestiaCP, an open-source Linux Web Server Control Panel. At the core of Alex's work lies his passion for Infrastructure as Code. He firmly believes in the principles of GitOps and lives by the mantra of "automate everything". This approach has consistently proven effective in enhancing the efficiency and reliability of the systems he manages. Beyond his professional endeavors, Alex has a broad range of interests. He enjoys traveling, is a football enthusiast, and maintains an active interest in politics.