How to update npm on Linux, Windows, and Mac


The short answer

To update npm to the latest version, run the following command:

npm install -g npm@latest

Or, if you need a specific version, specify it with @:

npm install -g npm@9.9.4

If you're on Windows or macOS, you can go to nodejs.org, download the latest installer, and run it. It comes bundled with npm. Alternatively, run winget upgrade -q NodeJS from the command line.

Once the install is complete, you can check your node version using the guide below.

How to check your nodejs version
Knowing what node.js and npm versions you’re running is important for troubleshooting and security. This guide will show you how to check it.

Why update npm?

Updating to the latest npm version ensures that you have the latest features, performance improvements, bug fixes, and security patches.

Additionally, there are critical security vulnerabilities in several older versions of Node.js and package dependencies. Updating npm will give you the tools to audit for and fix vulnerabilities in your project's dependencies.

How to check your npm version

If you want to check your current npm version or you're unsure whether it's installed or not, you can run the following command:

npm -v

You'll get an output such as:

11.6.1

How npm versions work

When looking at your version number, keep in mind that npm uses Major.Minor.Patch semantic versioning. Let's use npm 11.6.1 as an example:

  • 11: The major npm version, where there may be breaking changes, removed features, internal rewrites, etc.
  • 6: The minor npm version, which could include new features, non-breaking feature updates, new CLI options, performance improvements, and more.
  • 1: The patch version, addressing elements like bugs, vulnerabilities, internal stability fixes, and small behavior improvements.

It's important to keep this in mind when updating node for an important project. The further to the left the increase, the more likely it is to break your application.

What is the latest version of npm?

Fetching latest npm version…

How to update npm on Linux and macOS

The npm install command is one of the easiest ways to update your npm version on Linux or macOS.

npm install -g npm@latest

Let's break down the command in detail:

  • npm install: Invokes the install functionality of node package manager
  • -g: The global flag, which ensures npm and its packages update across your entire system, rather than the specific project you're working on
  • npm@latest: Specifies that npm is what you want to install and that you want it to be the @latest version.

How to install a specific node version

To choose a specific version or downgrade to an older version of npm, just type the version number after npm:

npm install -g npm@9.9.2

How to update npm using nvm

npm's documentation officially recommends using Node Version Manager (nvm) to install npm. Nvm can be very useful as it lets you easily switch between node versions. This can be very useful for testing new versions before pushing to production, for example. Using nvm rather than manually installing also ensures you have the correct permissions.

To update npm with nvm, first install it using:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

Then run the command it suggests to start using nvm instantly. Make sure to check the Github for the latest version of the above command.

Updating nvm is then really simple:

nvm install-latest-npm

This will update just npm for the current node version you have selected. You can alternatively update node and its associated npm version using:

nvm install node

How to use nvm to change npm version

As mentioned, npm allows you to seamlessly switch between node and npm versions. The and simplest way to change npm version with nvm is to switch to an associated node version:

nvm use 18

How to update npm for a specific Node.js version

You can update the npm version for a selected node version by installing it via npm with your target node version selected in nvm. For example:

nvm use 24.1.1
npm install npm@20

How to update npm using the n module

Updating npm using n helper module is outdated and only works on Linux and macOS, which is why it's not at the top of our list. However, it may be worth trying if you're having issues with the other methods. Run these commands to update npm:

sudo npm cache clean -f sudo npm install -g nsudo n latest

As with most methods, you don't necessarily need to update to the latest node.js version with the final command. You could instead use n stable or write a specific version number.

How to update npm manually

While it's not recommended because it can cause permisions errors,  you can also install npm manually using the official script. Just run the following command:

curl -qL https://www.npmjs.com/install.sh | sh

Updating npm with the Node.js installer on Windows

If you're using Windows, then updating applications via their dedicated installer will likely be the most familiar to you. This works for npm, too. Just:

  1. Download the latest Node.js Windows installer from the official website.

2. Double-click the executable to run the installation wizard.

3. Choose "Change" and make sure the npm feature is enabled.

4. Press "Next" until the installation is complete.