Command to remove all npm modules globally?

EhevuTov picture EhevuTov · Feb 14, 2012 · Viewed 422.6k times · Source

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

Answer

Kai Sternad picture Kai Sternad · Feb 14, 2012

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