Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command to remove all npm modules globally

Tags:

node.js

npm

Is there a command to remove all global npm modules? If not, what do you suggest?

like image 744
EhevuTov Avatar asked Feb 14 '12 20:02

EhevuTov


People also ask

How do I delete all NPM packages globally?

If you would like to remove all the packages that you have installed, you can use the npm -g ls command to find them, and then npm -g rm to remove them.

How do I uninstall all Global packages?

If you want to uninstall all global packages, then you need to name the packages one by one in the npm uninstall -g command. Run the npm list -g --depth=0 command to list the packages installed globally on your computer. That should uninstall all global packages for you.


2 Answers

The following command removes all global npm modules. Note: this does not work on Windows. For a working Windows version, see Ollie Bennett's Answer.

npm ls -gp --depth=0 | awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' | xargs npm -g rm 

Here is how it works:

  • npm ls -gp --depth=0 lists all global top level modules (see the cli documentation for ls)
  • awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' prints all modules that are not actually npm itself (does not end with /npm)
  • xargs npm -g rm removes all modules globally that come over the previous pipe
like image 102
Kai Sternad Avatar answered Oct 12 '22 13:10

Kai Sternad


For those using Windows, the easiest way to remove all globally installed npm packages is to delete the contents of:

C:\Users\username\AppData\Roaming\npm

You can get there quickly by typing %appdata%/npm in either the explorer, run prompt, or from the start menu.

like image 25
Ollie Bennett Avatar answered Oct 12 '22 14:10

Ollie Bennett