In this tutorial, you will learn how to uninstall npm packages in Node.js.
In Node.js, you can install packages in two ways:
npm uninstall
command is used to remove installed global and local npm packages.
Note: npm uninstall command has four aliases:
These aliases work the same, and you can use any four aliases in place of the npm uninstall
command.
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:
node_modules
folder.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.
Then, open the package.json
file and go through its dependencies section.
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.
Finally, open the package.json
file to confirm that the express package is removed.
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]
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