HomeBlogWeb DevelopmentHow to Install Specific Version of NPM Package?

How to Install Specific Version of NPM Package?

Published
20th Jun, 2024
Views
view count loader
Read it in
9 Mins
In this article
    How to Install Specific Version of NPM Package?

    Since 2009, Node.js has taken over the market and has become popular among developers for creating reliable systems and web-based applications. It has proved to be a boon for the JavaScript developers looking to create complex applications. Node.js has several features, but the core feature is the NPM (node package manager) that has helped JavaScript developers quickly and efficiently share and use packages, such as lodash, moment, and others.

    There is no limit on the number of packages available and how many have been downloaded. It lets the developers share and install the code from the package managers. The Node.js syllabus is vast and requires dedication to hone this skill. So, to better understand the working of Node.js and NPM, you must know the installation process. Then you can start creating unique applications.

    This article will discuss different methods to NPM install specific version of Node.js packages in detail.

    What is NPM? 

    The NPM (node package manager) is a default package manager for JavaScript runtime Node.js. The NPM has the following main parts- 

    • A CLI (command-line interface) allows the developers to publish and download the packages within your project. It lets the developers interact with NPM.
    • It also acts as an online repository well-known for hosting JavaScript packages.
    • The online website allows you to check for the required packages, create different user profiles, and improve the overall NPM experience for developers across the globe.

    How to Install Specific Version of NPM Package?

    Step 1: Before starting your project, you must ensure that you install the latest version of NPM to leverage all the new functionalities within your project. We first need to run the following command to check if we have NPM installed on my system. 

    Install a Specific Version of the NPM Package

    Step 2: Now, I can use the node to install a specific version of other packages required within your project. First, we will install the “moment” package as shown below. The Moment.js is a lightweight JavaScript development tool that lets you manipulate date and time.

    Install a Specific Version of the NPM Package

    Step 3: Now, to check what version of the “moment” package has been installed, we have to run the “NPM list” command.

    If you do not mention any version in the command, the NPM will fetch the latest version available. As you can see from the above image that the installed version is 2.29.3. But, there might be some cases where installing the latest packages with new changes can hamper your application, so you might require another version to perform perfectly.

    So, what command will work to run the NPM install command with a specific package version? You can use the below command to install specific NPM package version. You can mention the required version number after the package name is separated by the ‘@’ sign.

    Step 4: Now install the “2.25.3” version of the moment package. You can run the following command.

    npm install moment@2.25.3 //npm install specific version

    Install a Specific Version of the NPM Package

    The above command will install the package locally. But if you want a global moment package, you can use the “-g” flag with the install command. For example-

    npm install -g moment@2.25.3

    Install a Specific Version of the NPM Package

    Step 5: When installing the global package, you need admin access. As you can see, we have used sudo access to run the command. To list the global packages, you need to use the “-g” flag with the list command, as shown below.

    npm -g list

    Another example is to install an angular cli specific version. Installing angular-cli will take time.

    npm install -g @angular/cli@7

    How to Install Specific Version of NPM Package Using Yarn?

    Like the NPM install command, you can use the yarn command to add the package to your system. By default, it will install the package from the NPM registry. Else you have to specify another one in your package.json.

    • Also, you can use the yarm command to add the latest version of the NPM package. But first, we need to install the yarn to make it add the new version of the packages.
    npm install yarn

    Install a Specific Version of the NPM Package

    yarn add package-name
    yarn add react // yarn add specific version
    • But if you want to install NPM specific version of the package using the yarn command, then hit the following command.
    yarn add package-name@1.2.3 //yarn install specific version
    • Also, you can replace the tag with eta, next, or latest, as per your package requirement.
    yarn add package-name@tag

    How to Check Specific Version of NPM Installed Package?

    To list all the installed NPM packages, you can use the list command or ls for the short version.

    Install a Specific Version of the NPM Package

    It lists all the packages in a tree-like structure along with their dependencies. It is an easier option to check the version simultaneously as it is complex to check versions of packages individually. But, if you want detailed information on the installed NPM packages, you can just access the content of the package-lock.json file.

    So run the following command to get the details about packages.

    cat package-lock.json

    Identify Specific Version of Installed Package

    To get the list of the top-level packages without displaying their dependencies, you can add the -depth=0 flag. It will limit the search to the package name only.

    For example-

    npm ls -depth=0

    Identify Specific Version of Installed Package

    To check the globally installed packages, you can use the “-g” flag and the NPM list command. For example-

    npm list -g

    Identify Specific Version of Installed Package

    If you want to check the specific version for the installed package, you can hit the following command.

    npm list moment

    Identify Specific Version of Installed Package

    The above command has displayed the installed version only for the specific package mentioned.

    Whether you are a beginner or an experienced professional, you must learn from the best Full-stack Web Development course available in the market.

    How to Install Older Version of NPM Package?

    NPM uses the Semantic Versioning Specification, which is the convention rule to stipulate the versioning of the packages. You see three numbers separated by a dot whenever you install a package. As in the case of the installed moment package, it is 2.25.3 representing the major, minor, and patch versions, respectively. The semantic versioning specifies which version of the package to install. If you use a caret (^) character, it will install the minor version. You can use the (~) character to install the latest patch version of the specific package. 

    This is useful if you do not know the minor or patch version of a specific package that you want to install. You can simply prefix the version number with a caret character. 

    Also, if you are not sure about the latest minor version of the package with the latest security patches, you just need to prefix the version number with a tilde. For example-:

    How to Use Semantic Versioning to Specify Install Versions?

    The NPM uses the Semantic Versioning. It is a popular way of versioning the unique stages of the project. It is beneficial as it improves the code’s clarity and readability. If a new developer takes over, he can quickly go through different versions to understand the changes made earlier. It is a simple way to keep track of the changes that have been made.

    SemVer represents the version in the format- x.y.z. where- 

    • x specifies the major version, including the API changes and backwords incompatibility. 
    • Y specifies the minor version, including all the new and latest features implementation, enhanced framework or functionality, and backward compatibility. 
    • Z specifies the patch, including bug fixes, hotfixes, maintenance releases, and backward compatibility. 

    For example, we consider the 2.25.3 version (the latest) of the moment package. It specifies that “2” is the major version, “25” is the minor version, and “3” is the patch version. 

    Semantic versioning is crucial in helping the developers to keep track of the code and what has changed over time. Also, developers understand the changes and when and where the software has been updated. Not only this, developers can know which version is not backward compatible. 

    How to Install Node Using 'Homebrew'? [Step-by-Step]

    To install the Node with the help of the Homebrew, you need to follow the below steps.

    Step 1: First, install the Homebrew on your macOS. 

    Homebrew is a package manager for macOS that you need to install explicitly. Run the following command from the macOS terminal. 

    $curl -fsSL -o install.sh 
    https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh 

    Step 2: Check the installed Homebrew version. 

    To quickly check the installed version of the Homebrew, run the following command from the terminal. 

    $ brew -v 

    Step 3: Installing node with Homebrew 

    For installing Node on your macOS using Homebrew, run the following command. 

    $ brew install node 

    Step 4: Check the installed version of Node and NPM 

    You can check the installed version of Node and NPM using the below command. 

    $node -v 

    Also, use the following command to check the version of the NPM . 

    $npm -v 

    Step 5: Install a specific version of the node 

    If you specifically want to install a specific version of Node, you can use the following syntax.

    $ brew install node@<version_number> 

    How to Install Specific Version of NPM Package from GitHub?

    • Install from GitHub Repository: To install a package from a GitHub repository, enter the repository URL followed by the appropriate version or commit hash. 

    Example: 

    npm install github:user/repo#v1.2.3 

    In this example, replace ‘user’ with your own  Github username, ‘repo’ with the name of the repository, and v1.2.3 with the tag, branch, or commit hash you wish to install. 

    • Using Git URLs: For extra control, you can enter the Git URL directly into the npm install command, which is very handy for private repositories. 

    npm install git+https://github.com/user/repo.git#v1.2.3 

    This method is similar, but it uses the whole Git URL for the repository. 

    How to Know Package Versions Available? 

    When dealing with Node Package Manager (NPM), it is critical to understand the available package versions to ensure compatibility and access to required features or fixes. Here's a simple tutorial for tackling this: 

    • NPM Registry: The NPM registry is the major source of package information. You can obtain detailed information about a package, including available versions, by visiting the NPM webpage or using the command-line interface (CLI) with commands such as npm search or npm info <your package name>. 
    • Package.json: Your project's package.json file contains a list of dependencies and their version constraints. Running npm outdated can display which dependencies are out of date and which have the most recent versions available. This provides information about which packages require updating. 
    • Online Repositories: Many packages are maintained on version control systems like GitHub. Visiting the repository or its releases page can yield a list of available versions. In addition, the GitHub API can be used programmatically to retrieve version information.

    How to Use NPM?

    Now that we understand what NPM is, the question is how can you use it within your code. As it consists of CLI (command-line interface), we have to run CLI commands to interact with NPM. Below are some basic commands that will help you work with your project. 

    • NPM, init: creates the package.json file. If you are working on an application from scratch, it must be the first command that will run. If you install or remove the packages, Node, js will automatically update this file. 
    • NPM install: to install the required project dependencies within a package.json file. 
    • NPM install <package-name>: install a specific package from the NPM registry and store that package under the node_modules folder.
    • NPM install <package-name> –save: this command will install an NPM package within the dependencies in your package.json file. 
    • NPM install <package-name> –save-dev: it will install an NPM package under the devDependencies
    • npm uninstall <package-name>: this command will uninstall a specific package. 
    • NPM update <package-name>: it will update a specific package to the available latest version. 

    Points to be Considered While Working with Node and NPM

    Below are some points to consider working with Node and NPM. 

    • Ensure to have the latest version of Node installed on your system. If you do not have the latest version, run the following command to install a specific version of the node. 
    Install node //install specific node version
    • Then install the latest version of npm. If you do not want to work with the latest version, you can specify a specific version to install node and npm. Run the following to install a specific version of npm. 
    Install npm@<version> //install specific version of npm
    • To install the packages, you must have sudo access to do that. 

    Looking for Python training near me? Unlock the power of coding with Python, the versatile and in-demand programming language. Start your journey today and become a Python pro! 

    Conclusion

    Node.js has done wonders for JavaScript developers and reduces their challenges by going through several languages. It provides them with a sustainable development environment to use a single language and framework to do all the work.

    Node.js lets the developers build web-based applications with two-way connections, including both the server-side and client-side. Node.js has changed the way of pushing the applications in real-time over WebSocket. To central your skills in Node.js, you can refer KnowledgeHut Node.js syllabus

    So, if you are still confused about whether to go for Node.js or not, then there should be no doubt about it. It will make your life easy by streamlining your coding experience. 

    Frequently Asked Questions (FAQs)

    1How can I use a specific version of the node?

    You can run the following command. 

    npm run <version_number>

    or 

    npm exec <version_number> node

    • To use the installed nodeJs version in the system, run the following command. 

    nvm use system

    2How can I change the Node.js version?

    You can run the following commands to get a list of installed versions of Node.js. 

    npm –v 

    [PS /Users/praashibansal> npm –v

    8.5.5 

    PS /Users/praashibansal>

    As I have only one version installed of npm. But for switching between different versions of installed Node.js, you can run the following command. 

    nvm use <version_number>

    3Is it possible to specify the node version in package JSON?

    Follow the below steps to specify a node version within the package.json file. 

    1. First, choose the project name where you want to proceed. Your working project must have a package.json file. If not, you need to create one within the same directory as your project. 

    2. Now that you have a package.json file, you need to use the engines field to specify the Node version required by your project to function correctly. You can specify the version number of your choice and add the following code line within your package.json file. 

    Syntax: 

    “engines”: {

    “node”: “> or < =version_number”, 

    “npm”: “> or < =version_number” 

     },

    3. To specify the error whenever there is a mismatch in the node version, we will create a .npmrc file in the same folder as the package.json file. The .npmrc is a config file for the npm storing all required information on how the npm should run.

    To create a .npmrc file, add the below-mentioned code engine-strict=true. It will force display errors. 

    4How can I update an NPM module to a required specific version?

    You can run the npm update specific package command with a specific package name to update a specific package. 

    npm update moment 

    Also, in some cases, to update a package to a specific version, you must use the npm install command with a version number followed by the package name. 

    npm install react@15 

    Also, to download the latest stable version, you can use the @latestflag., as shown below. 

    npm install react@latest 

    5What is NPM?

    NPM is one of the largest open-source repositories of source code, packages, tools, and others. It lets the developers use all the code to create applications without working from scratch. 

    The NPM has the following main parts- 

    A CLI (command-line interface) allows the developers to publish and download the packages within your project. It lets the developers interact with npm.

    It also acts as an online repository well-known for hosting JavaScript packages.

    The online website allows you to check for the required packages, create different user profiles, and improve the overall npm experience for developers across the globe.

    Profile

    Aashiya Mittal

    Author

    Aashiya has worked as a freelancer for multiple online platforms and clients across the globe. She has almost 4 years of experience in content creation and is known to deliver quality content. She is versed in SEO and relies heavily on her research capabilities.

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming Web Development Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon