DEV Community

Mario
Mario

Posted on • Originally published at mariokandut.com on

How to uninstall npm packages?

Installing and uninstalling dependencies are core parts of working with any Node.js project. If you are just starting with Node.js, have a look at this article - how to install npm packages.

There are two ways to uninstall packages - manually or automatically.

Uninstalling dependencies

To uninstall a package, you have to remove it from your node_modules folder (that's where the code lives), and from package.json (listed there as a project dependency). If you only remove it in the node_modules folder and run npm install it will be reinstalled, and if you only remove the package entry in the package.json the package still exists in the node_modules folder.

But don't worry. The npm uninstall command will do both of it. Follow these steps for uninstalling dependencies:

  1. Identify the package you want to uninstall by looking in your package.json.
  2. Run the following command with the name of the package, or multiple packages separated by spaces:
npm uninstall <package1> <package2>
Enter fullscreen mode Exit fullscreen mode

After successfully running the command, the NPM CLI will uninstall the package and print information to the terminal about how many packages were removed. Uninstalling removes the specified package, and all the packages it used internally as dependencies.

  1. Verify that it has been removed in the package.json, this depends on your installed npm version. Since npm version 5 (released in 2017) installing or uninstalling a dependency will update the package.json automatically. Previously it was required when installing a package to add the --save flag to add the package entry in package.json.

Uninstall global packages

If you've installed a package globally, you can uninstall it by passing the -g flag when uninstalling. For example, if you have Gatsby CLI globally installed and want to remove it:

npm uninstall -g gatsby
Enter fullscreen mode Exit fullscreen mode

A helpful command is npm prune, which cleans extraneous packages in your node_modules folder. More details can be found in a future article.

TL;DR

  • Delete packages automatically with npm uninstall.
  • Avoid deleting packages manually.
  • Do not check your node_modules in version control.
  • Clean up node_modules with npm prune command.

Thanks for reading and if you have any questions , use the comment function or send me a message @mariokandut.

If you want to know more about Node, have a look at these Node Tutorials.

References (and Big thanks):

HeyNode, NPM Documentation - install, NPM Documentation - uninstall

Top comments (0)