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