Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Yarn equivalent for "npm dedupe"?

Tags:

npm

yarnpkg

Just tried out Yarn and indeed it's mega-fast. After running yarn in the console with a loaded package.json, it installed everything. I then ran npm dedupe expecting nothing much to happen since Yarn is so optimized, but it removed a ton of stuff. Is there some Yarn equivalent to npm dedupe?

Addition:

As noted in my comment below, I thought perhaps yarn install --flat might be an npm dedupe equivalent as the Yarn documentation describes it as "installing one and only one version of a package". So I played around with --flat and there's a couple things to note:

  1. It will prompt you to choose what version of a package to install when the tree results in requests for more than one version. I could imagine this being one hell of a task for larger projects. I just picked the latest version for each package I was prompted for and as you can imagine, trying to run the app resulted in cannot find module errors.
  2. I ran npm dedupe after doing a yarn install --flat and it still resulted in a ton of removals. This was a surprise to me.

So I'm taking it that npm dedupe is doing some other magic under the hood and I'm just naive about processes that I need not be concerned with. Perhaps it's just best to leave the tree alone and forget about --flat and dedupe altogether.

like image 623
The Qodesmith Avatar asked Sep 04 '25 02:09

The Qodesmith


1 Answers

It seems like even now Yarn doesn't do the greatest job of minimizing duplicate dependencies. Running yarn --flat forces there to be only one version of a dependency even when they may not be compatible, so that's not ideal. You can use the yarn-deduplicate package to minimize the number of duplicates, while still allowing some in cases where there are not overlapping requirements. yarn-deduplicate also has a --fail option which will return a non-zero (failing) status. This can be useful to run on CI to ensure that anybody who modifies dependencies doesn't introduce new duplicates.

like image 121
lobati Avatar answered Sep 07 '25 17:09

lobati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!