15

I built a monorepo using Lerna and Yarn workspaces.

Everything works fine but everytime I install a new dependency on a package (let's call him A) using:

yarn add <package_name>

Yarn adds it and then triggers the install script of all the packages in the monorepo, even the ones that A doesn't rely on.

It there anyway to avoid this? It takes a few moment to install them for no reason at all.

Erazihel
  • 7,295
  • 6
  • 30
  • 53
  • So you used both Lerna and Yarn packages? Why not just Lerna? Also, have you tried adding the package with Lerna and setting a scope? https://github.com/lerna/lerna/tree/master/commands/add – kbariotis Mar 28 '20 at 22:02
  • I use Lerna to publish and version my packages and Yarn workspaces to easily manage dependencies. I tried your suggestion and it didn't work. Thank you for trying :) – Erazihel Mar 28 '20 at 23:25

3 Answers3

12

Try adding to the specific workspace with:-

yarn workspace <workspace_name> add <package_name>

For some docs check here

Ezrqn Kemboi
  • 897
  • 1
  • 12
  • 19
  • That's exactly what this answer does. Adds a dependency to a specific workspace within the monorepo. – duhseekoh May 29 '20 at 17:46
  • This is the perfect way, you do regular `yarn install` in the root to install and make it available to each existing package. Another way can be, but it is not the best way, is cd(ing) to the package you want and run yarn there. – Norman Pleitez Jan 21 '23 at 18:28
6

Using scope add the package to the particular module.

lerna add some_package_1 --scope=some_module_x

More: https://github.com/lerna/lerna/tree/master/commands/add#readme

xdeepakv
  • 7,835
  • 2
  • 22
  • 32
4

You can try Yarn 2 with nodeLinker: node-modules in .yarnrc.yml. Yarn 2 guarantees to trigger rebuild only on packages that have their dependencies changed, this is something that was not guaranteed by Yarn 1. However there will still be a very rare case when seemingly unrelated packages be rebuilt if they are hoisted differently after adding new package, but this will happen very rarely.

Viktor Vlasenko
  • 2,332
  • 14
  • 16