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.