如何清理package.json中没有的包的node_modules文件夹?

本文翻译自:How to clean node_modules folder of packages that are not in package.json?

Assume I install project packages with npm install that looks into package.json for modules to be installed. 假设我使用npm install安装项目包,查看package.json以获取要安装的模块。 After a while I see that I don't need some specific module and remove its dependency from package.json . 过了一会儿,我发现我不需要一些特定的模块,并从package.json删除它的依赖。 Then I remove some other modules from package.json because they are not needed anymore and others are replaced with alternatives. 然后我从package.json删除了一些其他模块,因为它们不再需要了,而其他模块则替换为替代模块。

Now I want to clean node_modules folder so that only modules listed in package.json stay there and the rest must go, something like npm clean . 现在我想清理node_modules文件夹,以便只有package.json列出的模块保留在那里,其余的必须去,比如npm clean I know I can remove them manually but would like to have some nice ready to use sugar functionality for that. 我知道我可以手动删除它们但是希望有一些很好的准备使用糖功能。


#1楼

参考:https://stackoom.com/question/1qCSW/如何清理package-json中没有的包的node-modules文件夹


#2楼

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. 这将擦除当前文件夹中的所有已安装的包,并仅从package.json安装依赖项。 If the dependencies have been previously installed npm will try to use the cached version, avoiding downloading the dependency a second time. 如果先前已安装依赖项,则npm将尝试使用缓存版本,从而避免第二次下载依赖项。


#3楼

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. 无关的包是父包的依赖项列表中未列出的包。

See the docs: https://docs.npmjs.com/cli/prune 请参阅文档: https//docs.npmjs.com/cli/prune


#4楼

Have you tried npm prune? 你试过npm prune吗?

it should uninstall everything not listed in your package file 它应该卸载包文件中未列出的所有内容

https://npmjs.org/doc/cli/npm-prune.html https://npmjs.org/doc/cli/npm-prune.html


#5楼

Due to its folder nesting Windows can't delete the folder as its name is too long. 由于其文件夹嵌套,Windows无法删除该文件夹,因为其名称太长。 To solve this, install RimRaf: 要解决此问题,请安装RimRaf:

npm install rimraf -g

rimraf node_modules

#6楼

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 Fixer is 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值