是否有删除所有全局npm模块的命令?如果没有,你有什么建议?


当前回答

Windows:

rmdir /s /q "%appdata%/npm"

其他回答

只要把你的控制台:

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

这是我的工作……

我尝试了凯·斯特纳德的解决方案,但对我来说似乎并不完美。在最后一个awk之后,deps树本身留下了很多特殊的符号。

所以,我对Kai Sternad的解决方案进行了自己的修改(从cashmere的想法中得到了一点帮助):

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

NPM ls -gp——depth=0以可解析的格式列出所有全局安装的NPM模块:

/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}'从路径中提取模块名,形成所有全局安装模块的列表。

grep -vE '^(npm|)$'删除npm本身和空行。

Xargs -r NPM -g rm为列表中的每个模块调用NPM -g rm。

就像Kai Sternad的解决方案一样,它只能在*nix下工作。

对于一种更手动的方法,它不涉及文件浏览器,不关心安装在哪里,以后不太可能崩溃,100%跨平台兼容,并且由于额外的步骤而感觉更安全,请使用这种方法。

npm ls -g --depth=0 Copy output Paste into favorite code editor (I use vsCode. Great multi-cursor editing) Check for any packages you'd like to keep (nodemon, yarn, to name a few) Remove those lines Remove every instance of +-- or other line decorators Remove all the version information (eg '@2.11.4') Put all items on same line, space separated Add npm uninstall -g to beginning of that one line. Mine looks like npm uninstall -g @angular/cli @vue/cli express-generator jest mocha typescript bindings nan nodemon yarn, but I didn't install many packages globally on this machine. Copy line Paste in terminal, hit enter if not already added from the copy/paste Look for any errors in the terminal. Check npm ls -g to make sure it's complete. If something got reinstalled, rinse and repeat

其他仅使用cli的方法非常适合计算机管理员从相同的ssh或Puppet同时为100台几乎相同的计算机做一些事情。但如果你只做一次,甚至一年5次,这就容易多了。

sed的解决方案

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

对于那些使用Windows的人来说,删除所有全局安装的npm包的最简单的方法是删除以下内容:

C:\用户\用户名\应用数据\漫游\npm

您可以通过在资源管理器、运行提示符或开始菜单中键入%appdata%/npm快速到达那里。