Command to remove all npm modules globally?

2019-01-04 04:18发布

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

标签: node.js npm
17条回答
在下西门庆
2楼-- · 2019-01-04 05:04

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

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

EDIT: This command breaks with npm 3.3.6 (Node 5.0). I'm now using the following Bash command, which I've mapped to npm_uninstall_all in my .bashrc file:

npm uninstall `ls -1 node_modules | tr '/\n' ' '`

Added bonus? it's way faster!

https://github.com/npm/npm/issues/10187

How do you uninstall all dependencies listed in package.json (NPM)?

查看更多
干净又极端
3楼-- · 2019-01-04 05:04
npm ls -gp | awk -F/ '/node_modules/&&!/node_modules.*node_modules/&&!/npm/{print $NF}' | xargs npm rm -g
查看更多
老娘就宠你
4楼-- · 2019-01-04 05:07

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.

查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-04 05:08

in windows use "C:\Users\username\AppData\Roaming" path and manually remove npm folder

查看更多
虎瘦雄心在
6楼-- · 2019-01-04 05:08

Use this code to uninstall any package:

npm rm -g <package_name>
查看更多
登录 后发表回答