How to reinstall npm successfully

Sometimes, you may need to reinstall npm because of an error, or you may be unable to upgrade npm using the npm i -g npm command.

npm is bundled with Node.js and it doesnโ€™t have its own uninstaller.

To reinstall npm, you need to remove the Node.js program installed on your computer.

For Windows, you can remove Node.js from the Control Panel.

For Mac, Node and npm will be installed on the /usr/local/bin directory.

Delete the node and npm folders located there:

# ๐Ÿ‘‡ delete node folder
rm -rf  /usr/local/bin/node

# ๐Ÿ‘‡ delete npm folder
rm -rf  /usr/local/bin/npm

Once both folders are deleted, you can reinstall node and npm using the .pkg file for Mac.

If you install Node using Homebrew, then you can use brew command to uninstall it:

# ๐Ÿ‘‡ uninstall node and npm
brew uninstall node

# ๐Ÿ‘‡ install it again
brew install node

If youโ€™re using Linux Ubuntu, then you can remove Node using apt-get as shown below:

# ๐Ÿ‘‡ uninstall node for Ubuntu
sudo apt-get remove node

# ๐Ÿ‘‡ install it again
sudo apt-get install node

If you donโ€™t want to uninstall node, then you can try to download and install npm directly using the install.sh script from npmjs.com.

Open the terminal and run the following command:

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

But in my experience, the direct install will fail if you have an existing npm program as shown below:

$ curl -qL https://www.npmjs.com/install.sh | sh
...
fetching: https://registry.npmjs.org/npm/-/npm-8.13.2.tgz
removing existing npm
failed!

Seems npm direct installation wonโ€™t work for some reason.

You need to add the sudo command to the sh pipe as shown below:

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

If youโ€™re using ZSH, you may see the command above suspended like this:

zsh: suspended (tty input) sudo sh

When that happens, try to download the install.sh script first, then run it using the sudo command as follows:

# ๐Ÿ‘‡ download install.sh
curl -O https://www.npmjs.com/install.sh

# ๐Ÿ‘‡ run the script using sudo
sudo sh install.sh

# ๐Ÿ‘‡ after installation complete, remove the script
rm install.sh

The command above should help you to install npm directly.

Take your skills to the next level โšก๏ธ

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.