yarn autoclean


yarn autoclean [-I/--init] [-F/--force]

autoclean 命令通过从依赖文件中移除不需要的文件和文件夹来释放空间 它减少了你的项目 node_modules 里的文件,在包直接签入到版本控制系统的环境里很有用。

注意:此命令只被用于高级用例。 除非你遇到了 node_modules 里安装的文件数问题,不推荐使用此命令。 这个命令会永久删除在node_modules里的那些会造成包停止工作的文件

autoclean 默认被禁用。 若要启动,手动创建一个 .yarnclean 文件,或者运行 yarn autoclean --init 来创建默认内容的文件。 .yarnclean 文件应该被加入到版本控制。

当一个包中存在 .yarnclean 文件,autoclean 功能就会启用。清除执行时机为:

  • install
  • add
  • 如果运行了 yarn autoclean --force

清除通过读取 .yarnclean 文件中的每一行执行,每行作为一个要删除文件的 glob 模式。


-I/--init : 创建 .yarnclean 文件(如果尚不存在)并添加默认记录。 This file should then be reviewed and edited to customize which files will be cleaned. If the file already exists, it will not be overwritten.

-F/--force : 如果 .yarnclean 文件存在,运行其清除流程。如果该文件不存在,不做任何事。


When the yarn autoclean --init command is used to create a .yarnclean file, it will be pre-populated with a set of default items for deletion. This default list is a guess at what is likely not needed. It is impossible to predict all directories and files that are actually unnecessary for all existing and future NPM packages, so this default list may cause a package to no longer work.

It is highly recommended that you manually review the default entries in .yarnclean and customize them to fit your needs.



You decide all YAML and Markdown files in all your dependencies installed in node_modules can be safely deleted. You make a .yarnclean file containing:


然后再运行 yarn installyarn autoclean --force 。 The clean process will delete all *.yaml and *.md files within node_modules/ recursively (including nested transitive dependencies).