有没有一条命令可以删除所有全局npm模块?如果没有,你有什么建议?
npm list -g
将向您展示全局安装包的位置。
如果您想将它们输出到文件中:
npm list -g > ~/Desktop/npmoutputs.txt
npm rm -g
将会移除它们
npm rm -g <package_name>
npm 的 README.md 文件中写道:
If you would like to remove all the packages that you have installed, then you can use the
npm ls
command to find them, and thennpm rm
to remove them.To remove cruft left behind by npm 0.x, you can use the included
clean-old.sh
script file. You can run it conveniently like this:npm explore npm -g -- sh scripts/clean-old.sh
node_modules
中删除所有不需要的模块。请勿删除corepack
和npm
包,因为它们是Node.js所必需的。%USERPROFILE%\.nvm\{version}\node_modules
。
%USERPROFILE%
是您的用户文件夹。{version}
是您要删除其全局模块的 Node.js 版本。示例:C:\Users\Cappuccino\.nvm\19.8.1\node_modules
。
{installationPath}\{version}\node_modules
。
{installationPath}
是您安装 NVM for Windows 的位置。{version}
是您要删除其全局模块的 Node.js 版本。
示例:D:\Programs\NVM\v19.8.1\node_modules
。由于这是搜索中排名最高的答案,我将其发布在这里,因为这是我过去用来清理计算机并更换笔记本电脑的解决方案。
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
sudo npm uninstall npm -g
或者,如果失败了,获取npm源代码,并执行:
sudo make uninstall
手动删除所有与npm相关的内容:
rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/npm*
npm ls -gp | awk -F/ '/node_modules/&&!/node_modules.*node_modules/&&!/npm/{print $NF}' | xargs npm rm -g
rm -rf ${APPDATA//\\/\/}/npm
sed 解决方案
npm -gp ls | sed -r '/npm$|(node_modules.*){2,}/d; s:.*/([^/]+)$:\1:g' | xargs npm rm -g