Re-Installing Npm

Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Published: January 31, 2024

The short answer

In some cases, you might end up with a broken installation of npm, preventing you from managing your locally or globally installed packages.

To force reinstall the npm command-line tool on macOS and Linux, you can run the following curl command:

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

That will fetch and execute the official npmjs installation script.

Once executed, you can verify that the installation was successful by displaying the current version of npm using the -v option flag:

Bash
$ npm -v

Note that if you're facing incompatibility issues between your npm and node versions, or you simply want to update npm to a higher version, you can read our article on how to update npm globally or per project.

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:

Entering reinstall npm with curl in the AI Command Search will prompt a curl command that can then quickly be inserted into your shell by doing CMD+ENTER.

Reinstalling Node.js and npm using the terminal

Since, by default, Node.js and npm are bundled together, reinstalling node will also reinstall npm to its latest supported version, thus avoiding compatibility issues.

Reinstalling Node.js on MacOS with brew

To reinstall Node.js on MacOS with Homebrew, you can use the brew reinstall command:

Bash
$ brew reinstall node@ <version>

Where <version> is the current version of Node.js in use, which can be found using the node -v command.

For example:

Bash
$ node -v
v20.2.0
$ brew reinstall node@20

Reinstalling Node.js on Linux with apt

To reinstall Node.js on Linux with apt, you have to start by uninstalling it using the remove, purge, and autoremove commands:

Bash
$ sudo apt remove nodejs
$ sudo apt purge nodejs
$ sudo apt autoremove

Which will remove the current Node.js distribution from your system, as well as its configuration files, and any other unused packages that were installed with it.

Once you've done that, you can reinstall Node.js using the following command:

Bash
$ sudo apt install nodejs

Reinstalling Node.js on macOS, Linux, and Windows with nvm

If you are using nvm as your Node.js version manager, you can start by deactivating the desired Node.js version using the deactivate command:

Bash
$ nvm deactivate <version>

As you might otherwise get the following error message:

Bash
nvm: Cannot uninstall currently-active node version

You can then remove it from your system using the uninstall command:

Bash
$ nvm uninstall <version>

Then, reinstall it using the install command:

Bash
$ nvm install <version>

And finally set it as your current version using the use command:

Bash
$ nvm use <version>

For example, to reinstall Node.js version 20:

Bash
$ nvm deactivate 20
$ nvm uninstall 20
$ nvm install 20
$ nvm use 20

Reinstalling Node.js and npm using the Node installer

Whether you're using macOS, Linux, or Windows, the easiest way to reinstall Node.js using the graphical interface is to visit the official Node.js website at https://nodejs.org/en/download.

Then click on the desired distribution to download the GUI installer program.

Written by
Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Filed under

Related articles


How To Reinstall Packages With Npm

Brief guide to reinstalling npm packages using npm

List Installed Npm Packages

Learn how to list globally and locally installed packages with npm, including their dependencies.

Check Npm Package Version

Check an npm package version within your project

Install NPM Packages From GitHub

Check an npm package version within your project

Clear npm Cache

Learn how to clear the npm cache on Linux, macOS, and Windows.

Install Dev Dependencies With npm

Learn how to install and update dev dependencies using the npm install command.

Removing npm Packages

Learn how to remove packages locally, globally, and from the registry using the npm uninstall command.

Execute Packages With npx And npm

Discover the power of npm's npx tool, a developer's best friend for running packages without global installs.

How To Update NPM

Learn how to update npm to a specific version using the npm, nvm, or npx commands.