node.js 如何卸载 package.json (NPM) 中列出的所有依赖项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19106284/
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
How do you uninstall all dependencies listed in package.json (NPM)?
提问by qodeninja
If I have a package.json file defined in my application root and run npm install -git will install all the dependencies defined in package.json, globablly.
如果我在我的应用程序根目录中定义了一个 package.json 文件并运行npm install -g它,它将安装在 package.json 中定义的所有依赖项,globablly。
However, this doesn't seem to work in reverse.
然而,这似乎并没有反过来工作。
If I do npm uninstall -gin my application root it throws an error, expceting me to pass it a package name.
如果我npm uninstall -g在我的应用程序根目录中这样做,它会抛出一个错误,期望我向它传递一个包名。
Shouldn't this also uninstall the same packages I installed?
这不应该也卸载我安装的相同软件包吗?
Am I doing something wrong?
难道我做错了什么?
回答by jedmao
If using Bash, just switch into the folder that has your package.json file and run the following:
如果使用 Bash,只需切换到包含 package.json 文件的文件夹并运行以下命令:
for package in `ls node_modules`; do npm uninstall $package; done;
In the case of globally-installed packages, switch into your %appdata%/npmfolder (if on Windows) and run the same command.
如果是全局安装的软件包,请切换到您的%appdata%/npm文件夹(如果在 Windows 上)并运行相同的命令。
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!
加奖金?它的方式更快!
回答by Druubacca
This worked for me:
这对我有用:
command prompt or gitbash into the node_modules folder in your project then execute:
命令提示符或 gitbash 进入项目中的 node_modules 文件夹,然后执行:
npm uninstall *
Removed all of the local packages for that project.
删除了该项目的所有本地包。
回答by Sameer
For windows go to node_modules dir and run this in powershell
对于 Windows,转到 node_modules 目录并在 powershell 中运行它
npm uninstall (Get-ChildItem).Name
回答by chalo
I recently found a node command that allows uninstalling all the development dependencies as follows:
我最近发现了一个 node 命令,它允许卸载所有开发依赖项,如下所示:
npm prune --production
As I mentioned, this command only uninstalls the development dependency packages. At least it helped me not to have to do it manually.
正如我所提到的,此命令仅卸载开发依赖包。至少它帮助我不必手动完成。
回答by y0n1
Tip for Windows users: Run this PowerShell command from within node_modulesparent directory:
Windows 用户提示:从node_modules父目录中运行此 PowerShell 命令:
ls .\node_modules | % {npm uninstall $_}
回答by Gntem
Actually there is no option to do that, if you want to uninstall packages from package.jsonsimply do npm lson the same directory that package.jsonrelies and use npm uninstall <name>or npm rm <name>for the package you want to remove.
实际上没有选择这样做,如果你想卸载软件包,package.json只需npm ls在package.json依赖和使用的同一目录npm uninstall <name>或npm rm <name>要删除的软件包上执行即可。
回答by meem
// forcibly remove and reinstall all package dependencies
ren package.json package.json-bak
echo {} > package.json
npm prune
del package.json
ren package.json-bak package.json
npm i
This essentially creates a fake, empty package.json, calls npm pruneto remove everything in node_modules, restores the original package.json and re-installs everything.
这实际上创建了一个虚假的空 package.json,调用npm prune删除 node_modules 中的所有内容,恢复原始 package.json 并重新安装所有内容。
Some of the other solutions might be more elegant, but I suspect this is faster and exhaustive. On other threads I've seen people suggest just deleting the node_modules directory, but at least for windows, this causes npm to choke afterward because the bin directory goes missing. Maybe on linux it gets restored properly, but not windows.
其他一些解决方案可能更优雅,但我怀疑这是更快和详尽的。在其他线程上,我看到人们建议只删除 node_modules 目录,但至少对于 Windows,这会导致 npm 之后窒息,因为 bin 目录丢失。也许在 linux 上它可以正确恢复,但不是 Windows。
回答by Misaka_0x447f
- remove unwanted dependencies from package.json
npm i
- 从package.json 中删除不需要的依赖项
npm i
"npm i" will not only install missing deps, it updates node_modules to match the package.json
" npm i" 不仅会安装缺失的 deps,还会更新 node_modules 以匹配 package.json
回答by siOnzee
(Don't replicate these steps till you read everything)
(在阅读完所有内容之前不要重复这些步骤)
For me all mentioned solutions didn't work. Soo I went to /usr/liband run there
对我来说,所有提到的解决方案都不起作用。Soo 我去/usr/lib那里跑
for package in `ls node_modules`; do sudo npm uninstall $package; done;
But it also removed the npmpackage and only half of the packages (till it reached letter n).
但它也删除了npm包和只有一半的包(直到它达到字母 n)。
So I tried to install node again by the node guide.
所以我尝试通过节点指南再次安装节点。
# Using Ubuntu
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install -y nodejs
But it didn't install npmagain.
但它没有npm再次安装。
So I decided to reinstall whole node
sudo apt-get remove nodejsAnd again install by the guide above.
所以我决定重新安装整个节点
sudo apt-get remove nodejs并再次按照上面的指南进行安装。
Now is NPM again working but the global modules are still there. So I checked the content of the directory /usr/lib/node_modulesand seems the only important here is npm. So I edited the command above to uninstall everything except npm
现在 NPM 再次工作,但全局模块仍然存在。所以我检查了目录的内容,/usr/lib/node_modules似乎这里唯一重要的是npm. 所以我编辑了上面的命令来卸载除npm
for package in $(ls node_modules); do if [ "$package" != "npm" ]; then sudo npm uninstall $package; fi; done;
It removed all modules what were not prefixed @. Soo I extended the loop for subdirectories.
它删除了所有没有前缀的模块@。所以我扩展了子目录的循环。
for package in $(ls node_modules); do if [ ${package:0:1} = \@ ]; then
for innerPackage in $(ls node_modules/${package}); do
sudo npm uninstall "$package/$innerPackage";
done;
fi; done;
My /usr/lib/node_modulesnow contains only npmand linked packages.
我/usr/lib/node_modules现在只包含npm和链接的包。
回答by Ryan Lecha
Piggy-backing off of VIKAS KOHLIand jedmao, you can do this
的捎带关闭VIKAS KOHLI和jedmao,你可以这样做
single line version:
单行版本:
npm uninstall `ls -1 node_modules | grep -v ^@ | tr '/\n' ' '` `find node_modules/@* -type d -depth 1 2>/dev/null | cut -d/ -f2-3 | tr '\n' ' '`
multi-lined version:
多线版本:
npm uninstall \
`ls -1 node_modules | grep -v ^@ | tr '/\n' ' '` \
`find node_modules/@* -type d -depth 1 2>/dev/null | cut -d/ -f2-3 | tr '\n' ' '`

