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.

2
  • 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? github.com/lerna/lerna/tree/master/commands/add
    – kbariotis
    Commented Mar 28, 2020 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
    Commented Mar 28, 2020 at 23:25

3 Answers 3

12

Try adding to the specific workspace with:-

yarn workspace <workspace_name> add <package_name>

For some docs check here

2
  • That's exactly what this answer does. Adds a dependency to a specific workspace within the monorepo.
    – duhseekoh
    Commented May 29, 2020 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. Commented Jan 21, 2023 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

0
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.

1
  • I tried your suggestion but it appears that unfortunately the result is still the same... :/
    – Erazihel
    Commented Apr 11, 2020 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.