How to uninstall npm packages in Node.js?

In this tutorial, you will learn how to uninstall npm packages in Node.js.

In Node.js, you can install packages in two ways:

  1. Install packages locally in your Node.js project.
  2. Install packages globally.

npm uninstall command is used to remove installed global and local npm packages.

Note: npm uninstall command has four aliases:

  • npm remove
  • npm rm
  • npm un
  • npm unlink

These aliases work the same, and you can use any four aliases in place of the npm uninstall command.

Uninstall locally installed npm package

Use the npm uninstall command to remove a locally installed npm package.

$npm uninstall package-name

When you uninstall a locally installed package using the npm uninstall command, then NPM does two things:

  1. Delete all the files and folders of the package & its dependencies from the node_modules folder.
  2. Remove that package from the core dependencies of the project. It does this by removing the entry of that package from the dependencies section of the package.json.

Let's see how npm removes a package with the help of an example:

First of all, check how many packages are present in the node_modules folder of our project.

node_modules folder before uninstall

Then, open the package.json file and go through its dependencies section.

package.json file before uninstall

As you can see, that express package is installed in our project. Now, we will remove this package by running the npm uninstall command.

$npm unistall express

After this, check the folders present in the node_modules folder.

node_modules folder after uninstall

Finally, open the package.json file to confirm that the express package is removed.

package.json after uninstallation

Uninstall globally installed npm package

To uninstall a global npm package, pass the package name and -g flag to the npm uninstall command.

$npm uninstall -g package-name

For example, if you want to remove the nodemon package, then you run the following command:

$npm uninstall -g nodemon

If you get a permission error, then run the command with sudo privilege

$sudo npm uninstall -g nodemon

You can verify the package has been uninstalled by running the npm list command.

Before uninstalling the nodemon package, run the npm list command

$npm list -g --depth=0
/usr/local/lib
|-- [email protected]
|-- [email protected]
|-- [email protected]
|-- [email protected]
|-- [email protected]
|-- [email protected]

Run the npm list command after uninstalling the nodemon package.

$npm list -g --depth=0
/usr/local/lib
|-- [email protected]
|-- [email protected]
|-- [email protected]
|-- [email protected]
|-- [email protected]

Uninstall multiple packages

To uninstall multiple packages, pass more than one package separated by space to the npm uninstall command.

$npm uninstall package1 package2 package3

For example, the following code removes express and mongoose from your Node.js project.

$npm uninstall express mongoose

Recommended Posts