We typically install an npm package as follows:

1
npm install <package_name>

But how do we uninstall an npm package?

How to uninstall an npm package

This will uninstall an npm package that was installed in your project.

Just make sure to run the command from the root directory of your project.

1
npm uninstall <package_name>

This will remove the package from the node_modules directory and will also remove the reference to it from the package.json file.

How to uninstall a devDependency

If you have a package installed in the devDependency section of your package.json, then you can uninstall it as follows:

1
npm uninstall -D <package_name>

How to uninstall a global npm package

Sometimes we install npm packages globally, so that we can use them as command-line tools, or if we want them accessible to all projects.

This can be achieved as follows:

1
npm uninstall -g <package_name>

You may also ask these questions

What happens if I uninstall npm?

When you uninstall npm you are removing packages that have previously been installed by the npm install or npm i commands.

How do I uninstall npm alone?

If you would like to uninstall a specific npm package, then run the following command:

1
npm uninstall <package_name>

How uninstall npm react?

If you would like to uninstall a Remove Create React App:

1
2
3
4
5
# npm
npm uninstall -g create-react-app

# yarn
yarn global remove create-react-app

How do I uninstall and reinstall node and npm?

Delete the node and npm folders located there:

1
2
3
4
5
# 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:

1
2
3
4
5
# uninstall node and npm
brew uninstall node

# install it again
brew install node

If you are using Linux Ubuntu, then you can remove Node using apt-get as shown below:

1
2
3
4
5
# 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:

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

Typically, if you have an existing npm program installed already, the direct install will fail as shown below:

1
2
3
4
5
6
$ 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:

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

If you’re using ZSH, you may see the command above suspended like this:

1
# 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:

1
2
3
4
5
6
7
8
# 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