π Mastering Monorepos with Lerna + Yarn Workspaces
Managing multiple apps and libraries can be frustrating. Luckily, Lerna makes monorepos not just possible, but actually enjoyable.
πΉ Why Use Lerna?
Letβs say you have this structure:
/root
/app1
/app2
/shared-lib
package.json
lerna.json
Your package.json might look like this:
"workspaces": {
"packages": ["app1", "app2", "shared-lib"]
}
Now, add Lerna scripts:
"scripts": {
"start": "lerna run --parallel start",
"build": "lerna run build"
}
Run npm run start β both app1 and app2 start together. π
πΉ Monorepo vs Polyrepo
Monorepo: All apps + libs under one root
Polyrepo: Each app/lib in its own repo
With Lerna, the monorepo approach gets easier β single place to manage dependencies, build, and publish.
πΉ Bonus: Module Federation
Even though everything lives in one repo, with Webpack Module Federation, you can still load code at runtime across apps. Think: shared components between app1 and app2 without re-building everything.
π― Wrap-up
Lerna simplifies building, testing, and publishing packages in a monorepo.
Yarn Workspaces optimize dependency sharing.
Module Federation keeps things flexible at runtime.