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


当前回答

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

其他回答

sed的解决方案

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

对于使用Powershell的用户:

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

清除缓存。

npm cache clear

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

因为这是搜索中最热门的答案,所以我把它贴在这里,因为这是我过去用来清洁电脑切换笔记本电脑的解决方案。

cd ~/Documents # or where you keep your projects
find . -name "node_modules" -exec rm -rf '{}' +

来源:https://winsmarts.com/delete-all-node-modules-folders-recursively-on-windows-edcc9a9c079e

如果你安装了jq,即使没有grep/awk/sed,你也可以运行:

npm ls -g --json --depth=0 |
  jq -r '.dependencies|keys-["npm"]|join("\n")' |
  xargs npm rm -g

在Debian和衍生版本上,你可以安装jq:

sudo apt-get install jq