重要提示: 此中文文档针对的是 Yarn 2 版本。
有关 1.x 版本的中文文档,请点击进入 yarn.bootcss.com。
Yarn
yarn addyarn binyarn cache cleanyarn config getyarn config setyarn configyarn constraints queryyarn constraints sourceyarn constraintsyarn dedupeyarn dlxyarn execyarn infoyarn inityarn installyarn linkyarn nodeyarn npm infoyarn npm loginyarn npm logoutyarn npm publishyarn npm tag addyarn npm tag listyarn npm tag removeyarn npm whoamiyarn packyarn patch-commityarn patchyarn plugin import from sourcesyarn plugin importyarn plugin listyarn plugin removeyarn plugin runtimeyarn rebuildyarn removeyarn runyarn searchyarn set resolutionyarn set version from sourcesyarn set versionyarn stageyarn unplugyarn upyarn upgrade-interactiveyarn version applyyarn version checkyarn versionyarn whyyarn workspaceyarn workspaces focusyarn workspaces foreachyarn workspaces list

yarn dedupe

Deduplicate dependencies with overlapping ranges.

Usage

$> yarn dedupe [-s,--strategy #0] [-c,--check] [--json] ...

Examples

Dedupe all packages :

yarn dedupe

Dedupe all packages using a specific strategy :

yarn dedupe --strategy highest

Dedupe a specific package :

yarn dedupe lodash

Dedupe all packages with the @babel/* scope :

yarn dedupe '@babel/*'

Check for duplicates (can be used as a CI step) :

yarn dedupe --check

Details

Duplicates are defined as descriptors with overlapping ranges being resolved and locked to different locators. They are a natural consequence of Yarn's deterministic installs, but they can sometimes pile up and unnecessarily increase the size of your project.

This command dedupes dependencies in the current project using different strategies (only one is implemented at the moment):

  • highest: Reuses (where possible) the locators with the highest versions. This means that dependencies can only be upgraded, never downgraded. It's also guaranteed that it never takes more than a single pass to dedupe the entire dependency tree.

Note: Even though it never produces a wrong dependency tree, this command should be used with caution, as it modifies the dependency tree, which can sometimes cause problems when packages don't strictly follow semver recommendations. Because of this, it is recommended to also review the changes manually.

If set, the -c,--check flag will only report the found duplicates, without persisting the modified dependency tree. If changes are found, the command will exit with a non-zero exit code, making it suitable for CI purposes.

This command accepts glob patterns as arguments (if valid Idents and supported by micromatch). Make sure to escape the patterns, to prevent your own shell from trying to expand them.

In-depth explanation:

Yarn doesn't deduplicate dependencies by default, otherwise installs wouldn't be deterministic and the lockfile would be useless. What it actually does is that it tries to not duplicate dependencies in the first place.

Example: If foo@^2.3.4 (a dependency of a dependency) has already been resolved to foo@2.3.4, running yarn add foo@*will cause Yarn to reuse foo@2.3.4, even if the latest foo is actually foo@2.10.14, thus preventing unnecessary duplication.

Duplication happens when Yarn can't unlock dependencies that have already been locked inside the lockfile.

Example: If foo@^2.3.4 (a dependency of a dependency) has already been resolved to foo@2.3.4, running yarn add foo@2.10.14 will cause Yarn to install foo@2.10.14 because the existing resolution doesn't satisfy the range 2.10.14. This behavior can lead to (sometimes) unwanted duplication, since now the lockfile contains 2 separate resolutions for the 2 foo descriptors, even though they have overlapping ranges, which means that the lockfile can be simplified so that both descriptors resolve to foo@2.10.14.