Uninstalling npm packages with npm uninstall
By Flavio Copes
Learn how to uninstall an npm package with npm uninstall, removing it from node_modules and package.json, locally or globally with the -g flag.
To uninstall a package you have previously installed locally (using npm install <package-name>), run
npm uninstall <package-name>
from the project root folder (the folder that contains the node_modules folder).
For example, to remove the moment package:
npm uninstall moment
This deletes the package from the node_modules folder, and it also removes its reference from the package.json file and from package-lock.json. That’s the part you actually care about: node_modules is disposable, but package.json decides what gets installed next time.
It doesn’t matter which section the package was listed in. Modern npm removes the entry whether it was in dependencies or devDependencies. In old npm versions you had to pass the -D / --save-dev flag to remove a development dependency from the file, and the flag still works, but it’s no longer required:
npm uninstall -D prettier
You can also uninstall more than one package with a single command:
npm uninstall moment lodash
How to uninstall global packages
If the package is installed globally, you need to add the -g / --global flag:
npm uninstall -g webpack
You can run this command from anywhere on your system, because for global packages the folder where you currently are does not matter.
If you’re not sure what you have installed globally, list it first:
npm ls -g --depth=0
A pitfall: running it from the wrong folder
The most common way this goes wrong is running npm uninstall outside the project root. npm walks up looking for a package.json, so you might end up modifying a different project, or nothing useful at all. The command doesn’t fail loudly in that case.
The fix is to check where you are before running it, and verify the result after. Open package.json and confirm the entry is gone, or run:
npm ls moment
If the package was removed, npm reports it as (empty) or doesn’t list it. If it still shows a version number, you removed it somewhere else, or another package still depends on it and it survives in node_modules as a transitive dependency. That second case is fine: it’s no longer your dependency, npm keeps it only because something else needs it.
Related posts about node: