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
Command to remove all npm modules globally?
提问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
回答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 installedawk -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)?
回答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
回答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%

