Command to remove all npm modules globally?

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

This removes all npm modules globally:

npm -g ls | grep -v 'npm@' | awk '/@/ {print $2}' | awk -F@ '{print $1}' | xargs npm -g rm

The command works as follows:

npm -g ls lists all nodes globally. This will print something like:

usr/local/lib
└─┬ npm@1.1.1 
  ├── abbrev@1.0.3 
  ├── block-stream@0.0.4 
  ├── fast-list@1.0.2 
  ..
  • grep -v 'npm@' excludes npm itself

  • awk '/@/ {print $2}' splits all lines containing @ by its default record selector (blank). This takes care of the tree, so only lines like abbrev@1.0.3 remain.

  • awk -F@ '{print $1}' splits abbrev@1.0.3 into abbrev which is $1 and 1.0.3 which is $2 (we throw that away).

  • xargs npm -g rm then takes what is left (abbrev) and removes it.

Note: this solution will only work under *nix.

Update

Above solution doesn't work reliably with newer versions of npm since the structure of the tree appears to have changed. As Leonid Beschastny's answer correctly points out, there is a flag that prints the output of the ls command in machine parseable form which is preferable and makes the parsing much easier and reliable.

Updating the command above with this information, the new version of the command to remove all npm modules globally now looks like this:

npm ls -gp | awk -F/ '/node_modules/ && !/node_modules.*node_modules/ {print $NF}' | xargs npm -g rm

This version is inspired by Leonid's version, so please vote his answer up.

I tried Kai Sternad's solution but it seemed imperfect to me. There was a lot of special symbols left after the last awk from the deps tree itself.

So, I came up with my own modification of Kai Sternad's solution (with a little help from cashmere's idea):

npm ls -gp --depth=0 | awk -F/node_modules/ '{print $2}' | grep -vE '^(npm|)$' | xargs -r npm -g rm

npm ls -gp --depth=0 lists all globally-installed npm modules in parsable format:

/home/leonid/local/lib
/home/leonid/local/lib/node_modules/bower
/home/leonid/local/lib/node_modules/coffee-script
...

awk -F/node_modules/ '{print $2}' extracts module names from paths, forming the list of all globally-installed modules.

grep -vE '^(npm|)$' removes npm itself and blank lines.

xargs -r npm -g rm calls npm -g rm for each module in the list.

Like Kai Sternad's solution, it'll only work under *nix.

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 here quickly by typing %appdata% (either in explorer, run prompt, or start menu).

sudo npm list -g --depth=0. | awk -F ' ' '{print $2}' | awk -F '@' '{print $1}'  | sudo xargs npm remove -g

worked for me

  • sudo npm list -g --depth=0. lists all top level installed
  • awk -F ' ' '{print $2}' gets rid of ├──
  • awk -F '@' '{print $1}' gets the part before '@'
  • sudo xargs npm remove -g removes the package 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.

Just switch into your %appdata%/npm directory and run the following...

for package in `ls node_modules`; do npm uninstall $package; done;

For those using Powershell:

npm -gp ls --depth=0 | ForEach-Object { Get-Item $_ } | Where { $_.Name -ne 'npm' } | ForEach-Object { npm rm -g $_.Name }

To clear the cache:

npm cache clear

Use this code to uninstall any package:

npm rm -g <package_name>

npm ls -gp | awk -F/ '/node_modules/&&!/node_modules.*node_modules/&&!/npm/{print $NF}' | xargs npm rm -g

Well if you are on windows, and want to remove/uninstall all node_modules then you need to do following steps.

  1. Go to windows command prompt
  2. Navigate to node_modules directory (Not inside node_modules folder)
  3. Type below command and give it for 1-2 minutes it will uninstall all directories inside node_module

     rmdir /s /q node_modules
    

Hope this will help some one on windows

If you have jq installed, you can go even without grep/awk/sed:

npm ls -g --json --depth=0 |jq -r '.dependencies|keys-["npm"]|join("\n")' |xargs npm rm -g

sed solution

npm -gp ls | sed -r '/npm$|(node_modules.*){2,}/d; s:.*/([^/]+)$:\1:g' | xargs npm rm -g

It's as simple as: rm -rf ~/.npm