npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

yarn-berry-deduplicate

v6.1.1

Published

Deduplication tool for yarn.lock files

Downloads

90,444

Readme

yarn-berry-deduplicate

_This is a fork of yarn-deduplicate for yarn berry. _

Cleans up yarn.lock by removing duplicates.

Yarn berry includes yarn dedupe that currently only supports "highest" strategy. Follow this issue: https://github.com/yarnpkg/berry/issues/2297.

In the meantime, this is a fork of yarn-deduplicate that works with both highest and fewer (renamed "mostCommon") strategies as well as a new "fewerHighest" strategy which combines "mostCommon" and "highest".

Usage

yarn dlx yarn-berry-deduplicate

This will use the fewest then highest strategy to remove duplicated packages in yarn.lock.

If you do not specify the yarn.lock path, it defaults to yarn.lock.

Check all available options with:

yarn dlx yarn-berry-deduplicate --help

Note: you can apply multiple times deduplication and each time dedupe more. To prevent issues, try running yarn dedupe after each yarn-berry-deduplicate.


Duplicated packages

yarn.lock contains a list of all the dependencies required by your project (including transitive dependencies), and the actual package version installed to satisfy those dependencies.

For the context of this project, a "duplicated package" is a package that appears on multiple nodes of the dependency tree with overlapping version ranges but resolved to different versions.

For example, imagine that your project directly depends on lodash and babel, and babel depends on lodash as well. Specifically, your project depends on lodash@^1.0.0 and babel depends on lodash@^1.1.0. Because how the resolution algorithm works in Yarn, you might end up with two different copies of lodash (for example, version 1.0.1 and 1.2.0) in your project, even when 1.2.0 will suffice to satisfy both requirements for lodash. That's a "duplicated package".

It is important to note that we do not consider duplicated packages when the version ranges don't overlap. For example, if your project depends on underscore@^1.0.0 and underscore@^2.0.0. Your project will end up with two versions of underscore, and yarn-deduplicate won't change that.

When using yarn-deduplicate remember that it will change your dependency tree. There are certain code paths that now will run with a different set of dependencies. It is highly recommended that you review each change to yarn.lock. If the change is too big, use the flag --packages to deduplicate them gradually.

Why is this necessary?

Yarn documentation seems to suggest this package shouldn't be necessary. For example, in https://classic.yarnpkg.com/en/docs/cli/dedupe/, it says

The dedupe command isn’t necessary. yarn install will already dedupe.

This is, however, not exactly true. There are cases where yarn will not deduplicate existing packages. For example, this scenario:

  • Install libA. It depends on libB ^1.1.0. At this point, the latest version of libB is 1.1.2, so it gets installed as a transitive dependency in your repo

  • After a few days, install libC. It also depends on libB ^1.1.0. But this time, the latest libB version is 1.1.3.

In the above scenario, you'll end up with [email protected] and [email protected] in your repo.

Find more examples in:

Deduplication strategies

--strategy <strategy>

highest will try to use the highest installed version. For example, with the following yarn.lock:

library@^1.1.0:
  version "1.2.0"

library@^1.2.0:
  version "1.2.0"

library@^1.3.0:
  version "1.3.0"

It will deduplicate library@^1.1.0 and library@^1.2.0 to 1.3.0

fewer will try to minimize the number of installed versions by trying to deduplicate to the version that satisfies most of the ranges first. For example, with the following yarn.lock:

library@*:
  version "2.0.0"

library@>=1.1.0:
  version "3.0.0"

library@^1.2.0:
  version "1.2.0"

It will deduplicate library@* and library@>=1.1.0 to 1.2.0.

Note that this may cause some packages to be downgraded. Be sure to check the changelogs between all versions and understand the consequences of that downgrade. If unsure, don't use this strategy.

It is not recommended to use different strategies for different packages. There is no guarantee that the strategy will be honored in subsequent runs of yarn-deduplicate unless the same set of flags is specified again.

Progressive deduplication

--packages <package1> <package2> <packageN>

Receives a list of packages to deduplicate. It will ignore any other duplicated package not in the list. This option is recommended when the number of duplicated packages in yarn.lock is too big to be easily reviewed by a human. This will allow for a more controlled and progressive deduplication of yarn.lock.

--scopes <scope1> <scope2> <scopeN>

Receives a list of scopes to deduplicate. It will ignore any other duplicated package not in the list. This option is recommended when deduplicating a large number of inter-dependent packages from a single scope, such as @babel. This will allow for a more controlled and progressive deduplication of yarn.lock without specifying each package individually.

Excluding packages

--exclude <package1> <package2> <packageN

--exclude-scopes <scope1> <scope2> <scopeN>

With these commands you can exclude certain packages/scopes from the deduplication process. This is specially useful if you want to apply a different strategy for a scope, for example.

Pre-release versions

By default, yarn-deduplicate will only match pre-release versions if they share they share the same major, minor and patch versions (example: ^1.2.3-alpha.1 and 1.2.3-alpha.2 can be deduplicated, but ^1.2.3 and 1.2.4-alpha.1 can't). This matches the behaviour of semver.

To change this behaviour you can use the flag --includePrerelease. This will treat all pre-release versionas as if they were normal versions (^1.2.3 and 1.2.4-alpha.1 can be deduplicated).

Usage in CI

This tool can be used as part of a CI workflow. Adding the flag --fail will force the process to exit with status 1 if there are duplicated packages. Example:

# Print the list of duplicated packages and exit with status 1
yarn dlx yarn-berry-deduplicate --list --fail

# Deduplicate yarn.lock and exit with status 1 if changes were required
yarn dlx yarn-berry-deduplicate --fail

License

Copyright (c) 2022 Sergio Cinos and others. Apache 2.0 licensed, see LICENSE.txt file.