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


当前回答

对于使用Powershell的用户:

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

清除缓存。

npm cache clear

其他回答

sudo npm uninstall npm -g

或者,如果失败了,获取npm源代码,并执行:

sudo make uninstall

手动删除所有与npm相关的内容:

rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/npm*

对于一种更手动的方法,它不涉及文件浏览器,不关心安装在哪里,以后不太可能崩溃,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次,这就容易多了。

在Windows系统中,转到 “C:\Users{username}\AppData\Roaming”目录并手动删除npm文件夹

npm list -g

将显示全局安装包的位置。

如果你想把它们输出到一个文件: npm list -g > ~/Desktop/npmoutputs.txt

npm rm -g

会移除它们

如果你想删除你已经安装的所有包,你可以使用npm -g ls命令找到它们,然后npm -g rm删除它们。