πŸš€ 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.



Source link