node.js 全局删除所有 npm 模块的命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9283472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:11:13  来源:igfitidea点击:

Command to remove all npm modules globally?

node.jsnpm

提问by EhevuTov

Is there a command to remove all global npm modules? If not, what do you suggest?

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

回答by Kai Sternad

The following command removes all global npm modules. Note: this does not work on Windows. For a working Windows version, see Ollie Bennett's Answer.

以下命令删除所有全局 npm 模块。注意:这不适用于 Windows。对于可用的 Windows 版本,请参阅Ollie Bennett 的回答

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

Here is how it works:

下面是它的工作原理:

  • npm ls -gp --depth=0lists all global top level modules (see the cli documentation for ls)
  • awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}'prints all modules that are not actually npm itself (does not end with /npm)
  • xargs npm -g rmremoves all modules globally that come over the previous pipe
  • npm ls -gp --depth=0列出所有全局顶级模块(请参阅lscli 文档
  • awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}'打印所有实际上不是 npm 本身的模块(不以 结尾/npm
  • xargs npm -g rm全局删除来自前一个管道的所有模块

回答by Ollie Bennett

For those using Windows, the easiest way to remove all globally installed npm packages is to delete the contents of:

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

C:\Users\username\AppData\Roaming\npm

C:\Users\username\AppData\Roaming\npm

You can get there quickly by typing %appdata%/npmin either the explorer, run prompt, or from the start menu.

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

回答by Leonid Beschastny

I tried Kai Sternad's solutionbut it seemed imperfect to me. There was a lot of special symbols left after the last awkfrom the deps tree itself.

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

So, I came up with my own modification of Kai Sternad's solution (with a little help from cashmere's idea):

因此,我想出了自己对Kai Sternad解决方案的修改(在Cashmere 的想法中有一点帮助):

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

npm ls -gp --depth=0lists all globally-installed npm modules in parsableformat:

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}'extracts module names from paths, forming the list of all globally-installed modules.

awk -F/node_modules/ '{print $2}'从路径中提取模块名称,形成所有全局安装模块的列表。

grep -vE '^(npm|)$'removes npm itself and blank lines.

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

xargs -r npm -g rmcalls npm -g rmfor each module in the list.

xargs -r npm -g rm调用npm -g rm列表中的每个模块。

Like Kai Sternad's solution, it'll only work under *nix.

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

回答by cashmere

sudo npm list -g --depth=0. | awk -F ' ' '{print }' | awk -F '@' '{print }'  | sudo xargs npm remove -g

worked for me

对我来说有效

  • sudo npm list -g --depth=0.lists all top level installed
  • awk -F ' ' '{print $2}'gets rid of ├──
  • awk -F '@' '{print $1}'gets the part before '@'
  • sudo xargs npm remove -gremoves the package globally
  • sudo npm list -g --depth=0.列出所有顶级安装
  • awk -F ' ' '{print $2}'去掉 ├──
  • awk -F '@' '{print $1}'获取 '@' 之前的部分
  • sudo xargs npm remove -g全局删除包

回答by Kedar Vaidya

For those using Powershell:

对于那些使用Powershell 的人

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

To clear the cache:

要清除缓存:

npm cache clear

回答by jedmao

Just switch into your %appdata%/npmdirectory and run the following...

只需切换到您的%appdata%/npm目录并运行以下命令...

for package in `ls node_modules`; do npm uninstall $package; done;

EDIT: This command breaks with npm 3.3.6 (Node 5.0). I'm now using the following Bash command, which I've mapped to npm_uninstall_all in my .bashrc file:

编辑:此命令与 npm 3.3.6(节点 5.0)中断。我现在正在使用以下 Bash 命令,该命令已映射到我的 .bashrc 文件中的 npm_uninstall_all:

npm uninstall `ls -1 node_modules | tr '/\n' ' '`

Added bonus? it's way faster!

加奖金?它的方式更快!

https://github.com/npm/npm/issues/10187

https://github.com/npm/npm/issues/10187

How do you uninstall all dependencies listed in package.json (NPM)?

如何卸载 package.json (NPM) 中列出的所有依赖项?

回答by pradeep karunathilaka

in windows go to "C:\Users{username}\AppData\Roaming" directory and manually remove npm folder

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

回答by Bill

If you would like to remove all the packages that you have installed, you can use the npm -g lscommand to find them, and then npm -g rmto remove them.

如果要删除所有已安装的软件包,可以使用该npm -g ls命令查找它们,然后npm -g rm将其删除。

回答by eush77

If you have jqinstalled, you can go even without grep/awk/sed:

如果你安装了jq,你甚至可以不用 grep/awk/sed:

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

On Debian and derived you can install jqwith:

在 Debian 和派生上,您可以使用以下命令安装jq

sudo apt-get install jq

回答by bvj

OS not specified by OP. For Windows, this script can be used to nuke the local and the user's global modules and cache.

OP 未指定操作系统。对于Windows,此脚本可用于破坏本地和用户的全局模块和缓存。

I noticed on linuxthat the global root is truly global to the system instead of the given user. So deleting the global root might not be a good idea for a shared system. That aside, I can port the script to bashif interested.

我注意到在linux上,全局根目录对于系统来说是真正全局的,而不是给定的用户。因此,对于共享系统,删除全局根目录可能不是一个好主意。除此之外,如果有兴趣,我可以将脚本移植到bash

For Windows, save to a cmd file to run.

对于Windows,保存到 cmd 文件以运行。

@ECHO OFF
SETLOCAL EnableDelayedExpansion 
SETLOCAL EnableExtensions

SET /A ecode=0

:: verify
SET /P conf="About to delete all global and local npm modules and clear the npm cache. Continue (y/[n])?
IF /I NOT "%conf%"=="y" (
  ECHO operation aborted
  SET /A ecode=!ecode!+1
  GOTO END
)

:: wipe global and local npm root
FOR %%a IN ("" "-g") DO (

  :: get root path into var
  SET cmd=npm root %%~a
  FOR /f "usebackq tokens=*" %%r IN (`!cmd!`) DO (SET npm_root=%%r)

  :: paranoid
  ECHO validating module path "!npm_root!"
  IF "!npm_root:~-12!"=="node_modules" (
    IF NOT EXIST "!npm_root!" (
      ECHO npm root does not exist "!npm_root!"
    ) ELSE (
      ECHO deleting "!npm_root!" ...
      :: delete
      RMDIR /S /Q "!npm_root!"
    )
  ) ELSE (
      ECHO suspicious npm root, ignoring "!npm_root!"
  )
)

:: clear the cache
ECHO clearing the npm cache ...
call npm cache clean

:: done
ECHO done

:END

ENDLOCAL & EXIT /b %ecode%