Npm stands for “Node Package Manager” and is used for managing the node.js packages and modules using the command line. It is written in JavaScript and was developed and maintained by Isaac Z. Schlueter.
Whenever you install Node.js on your system, npm is installed by default. After installing Node.js, you can easily install the required Node modules and packages using npm.
A package in Node.js contains all the necessary files for a module or JavaScript library that can be used in a project. To manage dependencies for a project, developers use a file called “package.json.”Within this file, each dependency is defined along with a range of valid npm versions that are compatible with it.
Moreover, npm helps manage the peer dependencies for a specific project and install JavaScript libraries globally on your system. It also manages the package versions, meaning you can also install any previous or later version of a library based on your project requirements. In case you don’t provide the specific version to install, npm will, by default, install the latest version of that package. In addition, you can uninstall or update old packages using npm if they are not required further.
In this guide, we will go through the steps on how to update the npm version using the command line on Windows and Linux distributions.
NPM RESOURCES
Npm is an open-source package manager for Node.js, comprising three main components. Firstly, you can visit the official npm website to discover packages and access their documentation. This resource also enables you to publish and share your packages on the platform.
Secondly, the npm registry contains millions of packages and a comprehensive database. Developers can both download packages from the registry and publish their packages to it.
Lastly, the CLI (Command Line Interface) resource allows you to interact with npm, facilitating tasks such as installing, uninstalling, updating, and managing project dependencies.
HOW TO UPDATE THE NPM VERSION ON LINUX?
To update the npm package on Linux, follow the steps below:
STEP 1: ADD THE PPA APT REPOSITORY
You can install the latest Node.js and npm packages using the “PPA:chris-lea” repository. However, you need to add this repository to your Ubuntu system.
$ sudo add-apt-repository ppa:chris-lea/node.js
The above command will include the required PPA repository in your system.
STEP 2: UPDATE PACKAGES INDEX
Now, update the Ubuntu apt repositories using the following command:
$ sudo apt update
STEP 3: INSTALL NODE.JS AND NPM
Once you refresh the Ubuntu Apt packages index, install Node.js and npm using the following commands:
$ sudo apt install node.js npm
The above command will install the latest Node.js and npm versions on your Linux system. Using all of the above commands, you can also update npm on Linux VPS hosting.
HOW TO UPDATE THE NPM VERSION ON WINDOWS?
You can update the npm version on Windows using the following steps:
Before upgrading the npm version, it is recommended that you first check the installed npm version using the command prompt by running the following command:
> npm --version
As you can see in the image above, the npm v9.6.7 version is running on this system.
STEP 1: SET THE EXECUTION POLICY
Open the PowerShell from the Windows start menu. Run the below-given command to set the execution policy for the current user:
> Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
The above command will change the PowerShell execution policy to "Unrestricted" for the current user.
STEP 2: INSTALL THE “NPM-WINDOWS-UPGRADE” PACKAGE
Install the “npm-windows-upgrade” package globally using Node Package Manager:
> npm install -g npm-windows-upgrade
STEP 3: UPDATE THE NPM VERSION
Execute the below-given command to upgrade npm to the latest version on your Windows operating system.
> npm-windows-upgrade
After executing the above command, you will be prompted to choose an npm version for installation. You can navigate through the available versions using the arrow keys to move both up and down, and once your desired version is highlighted, press the "Enter" key to confirm your selection.
After successfully installing the desired npm version, you will see the following output on your PowerShell window:
HOW TO INSTALL NODE.JS?
To effectively manage Node.js packages using npm, it's essential to ensure that you have the latest or stable version of npm running on your system. Npm is an integral part of Node.js and is automatically installed when you install Node.js. Therefore, there's usually no need to install npm separately.
If you're interested in installing Node.js, you can visit the official website for the download. Once you've successfully installed Node.js, you can verify the installed Node.js and npm versions using the following commands:
> node --version
> npm --version
The above commands will display the respective versions of Node.js and npm that are currently installed on your system.
HOW TO UPDATE NPM USING COMMANDS?
To update npm, you can use the following syntax:
> npm update [-g] [<pkg>...]
In this syntax, the “-g” is used for globally updating and <pkg> indicates the package name.
You can update the npm version and manage Node.js packages using the several commands that we have listed below:
UPDATE NPM VERSION USING THE ”UPDATE” COMMAND
You can easily update all locally outdated packages by running the command “npm update” with no arguments. Likewise, if you wish to update global packages, you can achieve this by executing the following command:
> npm update -g
UPDATE NPM USING THE “NPM@LATEST” COMMAND
To update the Node Package Manager (NPM) to the latest version, you can execute the command “npm@latest.” This command is effective for updating npm on Linux systems as well.
> npm install npm@latest -g
Again, check the installed npm version:
> npm --version
As you can see, the npm version has been updated to its latest version.
UPDATE NPM USING THE “NPM@NEXT” COMMAND
Using the “npm@next” command, you can update the npm version. This command is also helpful for Linux systems:
> npm install -g npm@next
Occasionally, npm may undergo updates that render it incompatible with the currently installed version, causing installation issues.
CONCLUSION
Node.js is a widely used platform for building various applications, and it's crucial to keep your development tools up-to-date to enhance productivity. In this guide, we have demonstrated how to update the npm version on Windows and Linux systems. It’s important to note that npm comes bundled with Node.js installations, so there's no need to install it separately on your system.
We also provided several commands that you can use to update npm to the latest stable version. This essential tool, Node Package Manager, also helps in managing packages, modules, and dependencies for Node.js projects. Finally, keep in mind that it’s recommended to use the most recent stable version of npm to take advantage of the latest features.