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.
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 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.
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.
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.