node.js 如何清理不在 package.json 中的包的 node_modules 文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21122342/
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 to clean node_modules folder of packages that are not in package.json?
提问by Sergei Basharov
Assume I install project packages with npm installthat looks into package.jsonfor modules to be installed. After a while I see that I don't need some specific module and remove its dependency from package.json. Then I remove some other modules from package.jsonbecause they are not needed anymore and others are replaced with alternatives.
假设我安装了项目包,npm install它会查找package.json要安装的模块。过了一会儿,我发现我不需要某些特定的模块并从package.json. 然后我删除了一些其他模块,package.json因为不再需要它们,而其他模块则替换为替代品。
Now I want to clean node_modulesfolder so that only modules listed in package.jsonstay there and the rest must go, something like npm clean. I know I can remove them manually but would like to have some nice ready to use sugar functionality for that.
现在我想清理node_modules文件夹,以便只有列在package.json那里的模块留在那里,其余的必须去,比如npm clean. 我知道我可以手动删除它们,但想要一些很好的准备使用糖功能。
回答by David Sherret
I think you're looking for npm prune
我想你正在寻找 npm prune
npm prune [<name> [<name ...]]This command removes "extraneous" packages. If a package name is provided, then only packages matching one of the supplied names are removed.
Extraneous packages are packages that are not listed on the parent package's dependencies list.
npm prune [<name> [<name ...]]此命令删除“无关的”包。如果提供了包名称,则仅删除与提供的名称之一匹配的包。
无关包是未在父包的依赖项列表中列出的包。
See the docs: https://docs.npmjs.com/cli/prune
回答by adamduren
You could remove your node_modules/ folder and then reinstall the dependencies from package.json.
您可以删除 node_modules/ 文件夹,然后从 package.json 重新安装依赖项。
rm -rf node_modules/
npm install
This would erase all installed packages in the current folder and only install the dependencies from package.json. If the dependencies have been previously installed npm will try to use the cached version, avoiding downloading the dependency a second time.
这将删除当前文件夹中所有已安装的包,只安装 package.json 中的依赖项。如果之前已经安装了依赖项,npm 将尝试使用缓存的版本,避免再次下载依赖项。
回答by user3844078
Due to its folder nesting Windows can't delete the folder as its name is too long. To solve this, install RimRaf:
由于其文件夹嵌套 Windows 无法删除该文件夹,因为它的名称太长。要解决此问题,请安装 RimRaf:
npm install rimraf -g
rimraf node_modules
回答by Ajay Kotnala
simple just run
简单运行
rm -r node_modules
in fact, you can delete any folder with this.
事实上,你可以用这个删除任何文件夹。
like rm -r AnyFolderWhichIsNotDeletableFromShiftDeleteOrDelete.
像rm -r AnyFolderWhichIsNotDeletableFromShiftDeleteOrDelete。
just open the gitbashmove to root of the folder and run this command
只需打开gitbashmove 到文件夹的根目录并运行此命令
Hope this will help.
希望这会有所帮助。
回答by Ankit Parmar
First globally install rimraf
首先全局安装rimraf
npm install rimraf -g
go to the path using cmd where your node_modules folder and apply below command
使用 cmd 转到 node_modules 文件夹所在的路径并应用以下命令
rimraf node_modules
回答by Christoffer Bo Petersen
Have you tried npm prune?
你试过 npm prune 吗?
it should uninstall everything not listed in your package file
它应该卸载包文件中未列出的所有内容
回答by Giuseppe B
from version 6.5.0 npm supports the command clean-installto hard refresh all the packages
从版本 6.5.0 npm 支持clean-install硬刷新所有包的命令
回答by Amitesh Singh
I have added few lines inside package.json:
我在 package.json 中添加了几行:
"scripts": {
...
"clean": "rmdir /s /q node_modules",
"reinstall": "npm run clean && npm install",
"rebuild": "npm run clean && npm install && rmdir /s /q dist && npm run build --prod",
...
}
If you want to cleanonly you can use this rimraf node_modulesor rm -rf node_modules.
如果您只想clean使用 thisrimraf node_modules或rm -rf node_modules.
It works fine
它工作正常
回答by Osify
For Windows User, alternative solution to remove such folder listed here: http://ask.osify.com/qa/567
对于 Windows 用户,删除此处列出的此类文件夹的替代解决方案:http: //ask.osify.com/qa/567
Among them, a free tool: Long Path Fixeris good to try: http://corz.org/windows/software/accessories/Long-Path-Fixer-for-Windows.php
其中,有一个免费工具:Long Path Fixer很好尝试:http: //corz.org/windows/software/accessories/Long-Path-Fixer-for-Windows.php
回答by robertovg
The best article I found about it is this one: https://trilon.io/blog/how-to-delete-all-nodemodules-recursively
我发现的关于它的最好的文章是这篇:https: //trilon.io/blog/how-to-delete-all-nodemodules-recursively
All from the console and easy to execute from any folder point.
全部来自控制台,并且易于从任何文件夹点执行。
But as a summary of the article, this command to find the size for each node_modulefolder found in different projects.
但作为文章的总结,此命令用于查找node_module在不同项目中找到的每个文件夹的大小。
find . -name "node_modules" -type d -prune -print | xargs du -chs
And to actually remove them:
并实际删除它们:
find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;
The article contains also instructions for windows shell.
该文章还包含有关 windows shell 的说明。

