[node.js] Command to remove all npm modules globally?


Answers

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).

Question

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




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

On Debian and derived you can install jq with:

sudo apt-get install jq



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




if you have Intellij Webstorm you can use its built-in graphical package manager.

open it as root and create an emtpy project. go to

File > Settings > Language and Frameworks > Node.js and NPM

there you will see all the installed packages. Uninstalling is easy, you can select and deselect any package you want to uninstall, Ctrl+a woks as well.




Use this code to uninstall any package:

npm rm -g <package_name>



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



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



Links



Tags

node.js node.js   npm npm