Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Some features are removed as a tool gets new updates and versions with the latest upgrades rolling out. Sometimes our development environment and habits are tuned to those old features, and the more recent versions make our environment redundant. There may be other reasons, but sometimes it becomes crucial to install lower versions on packages and tools.
The ability to install version-specific packages and get downgraded and upgraded itself this easily is what makes NPM such a robust tool.
What is NPM?
Before starting to learn how to upgrade or downgrade NPM, or other Javascript packages using it, it is vital to establish what NPM is. NPM stands for Node Package Manager. It is a Javascript package manager and the default for the runtime environment NodeJS. NPM is installed when NodeJS is installed on a machine. It comes with a command-line interface (CLI) used to interact with the online database of NPM. This database is called the NPM Registry, and it hosts public and private ‘packages’. To add or update packages, we use the NPM CLI to interact with this database.
The latest version of NPM is version 8.1.1, while the first version dates back to November 2010.
Managing NPM Versions
In this blog section, we will see how to upgrade NPM or install a specific lower version.
Before deciding what version to use, we should thoroughly understand what changes have rolled out with different NPM versions. For example, when NPM version 7 was released, it came with two big roll-outs - Workspaces and the feature of auto-installing Peer Dependencies. The complete version chart of NPM is available officially, and you can go through it here.
Firstly, we need to check what version of NPM is installed on our PC. We can simply do this by running the following command:
On Windows:
npm -v
On Linux:
npm --version
On running this command, we would get back the version of npm installed on our PC.
Output on Windows showing version 8.1.1
Upgrade NPM
To upgrade NPM, we can follow two paths. One would be using the NPM update command, and the other would be to install the latest available version of NPM manually.
If we run the update command with the global flag, it would update all instances of NPM globally.
npm update -g
The other way of updating NPM to its latest version is using the package@latest command.
We run it on our terminal like:
npm install -g npm@latest
The following image shows the NPM versions before and after updating NPM.
Output on Windows Command Prompt
Install a Lower npm Version
Sometimes developers upgrade their development environments but feel that the newer versions of tools do not quite meet their development needs, which results in them wanting to go back or downgrade the versions.
NPM is a very powerful package manager. It enables developers to install whichever version of a package they want. The process to do this is quite simple.
To downgrade npm to whichever version, we just use the npm@version-number command.
On the terminal, implement the following command:
npm install -g npm@version-number
The output of this command on the windows command prompt, along with the versions before and after the command, are shown below:
Output on Windows Command Prompt
In the example above, originally, the version of NPM installed on the machine was 8.1.1. After running the command, the version was updated, and NPM was downgraded to version 6.14.10.
Managing Package Versions
NPM’s robust nature comes with its capability to manage package versions. We can easily view, upgrade or install specific versions of packages as long as they are available on the NPM registry.
Viewing the Installed Version of a Package
Every project has its own package-lock.json file. This file locks the versions of all packages used in the project. The version of every package used in the project is listed there and can be simply checked.
A sample package-lock file showing versions of all packages
We can also check the versions using the terminal. The command to check the versions of packages installed is:
To check the version of all packages in a project:
// This command should be run inside the project directory
npm list
To check the version of all packages globally:
npm list -g
To check the version of a specific package, we use:
npm list package-name
To read more about where NPM installs packages, you can read this blog.
Updating Versions to Specific Versions
To change the version of an installed package to a specific version, we reinstall the package while specifying what version of the package we want to install. This updates the current version of the package.
npm install package@version
Frequently Asked Questions
How can we use the Linux bash to update NPM? Firstly we have to clear the cache memory associated with NPM. Then we reinstall the package. This process is completed using:
// Clean the cache of npm first
sudo npm cache clean -f
// Install n module of npm
sudo npm install -g n
// Select the version of node to install: stable or latest
sudo n stable
2. What is the use of the “view” command? The view command is used on the bash to view the entire version history and other details of a particular package.
npm view package-name
3. How to see the version of the top-level packages? Top-level packages are those which the user asks NPM to install, or simply, the packages listed in a package-lock.json file. To check their version, we add another flag in our command.
// The depth=0 flag restricts the search to top level packages
npm list --depth=0
Key Takeaways
In this blog, we learned how to manage npm versions of the node package manager and the various packages or dependencies that are needed to be installed for our projects. We can upgrade, downgrade to change to a specific npm version just by using the npm CLI.