删除所有全局安装的npm模块的命令

465

有没有一条命令可以删除所有全局npm模块?如果没有,你有什么建议?

31个回答

0
npm list -g

将向您展示全局安装包的位置。

如果您想将它们输出到文件中:
npm list -g > ~/Desktop/npmoutputs.txt

npm rm -g

将会移除它们


0
请使用这个代码来卸载任何软件包:
npm rm -g <package_name>

7
问题明确表示“全部”,而不是特定的软件包名称。 - Polv

0

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 then npm 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

0
在 macOS 中,我相信您可以直接删除用户目录中的 .npm-global 文件夹。
macOS 用户目录中的 .npm 和 .npm-global 文件夹:
.npm and .npm-global folders in macOS User directory

0
如果您正在使用NVM for Windows,则需要在包含您想删除的全局模块的Node.js版本的node_modules中删除所有不需要的模块。请勿删除corepacknpm包,因为它们是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

0

0
sudo npm uninstall npm -g

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

sudo make uninstall

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

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

0
npm ls -gp | awk -F/ '/node_modules/&&!/node_modules.*node_modules/&&!/npm/{print $NF}' | xargs npm rm -g

0
如果您在Windows上安装了MSYS:
rm -rf ${APPDATA//\\/\/}/npm

-1

sed 解决方案

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接