JSBits #6: Lerna vs. Yarn workspaces

Captain's log, stardate d75.y38/AB

The other day, one of our developers messaged me on Slack about a tool called Lerna. I've recommended yarn workspaces over Lerna in the past, but now I want to be more specific about why.

Museum of fine arts - Photo by Tony Reid

My general take is:

Use yarn workspaces if you're building something with several packages (like a big application) but, if you intend to publish that packages into a registry (like a library or a framework) use lerna instead.

Splitting the code into small modules is a great way to write clean, decoupled, high quality and well-tested code. The JavaScript ecosystem knows it well. But - surprise! - it has its drawbacks.

At development time, you have to keep up to date the dependencies of each module with each other in order to "see" the changes. That's what yarn workspace and lerna do. In short, they put symlinks inside node_modules, so when you modify a package, the dependent packages can see it.

But there's another problem: when a package A depends on a package B that increases its version number (because it's modified), then package A should change the version number, too. Coordinating several changing version numbers is hard, and that's what lerna is designed for.

Happy monorepo hacking!

Daniel Gómez

Daniel Gómez

Dani started playing with computers with his Oric 1, at least 100 years ago. He now juggles his programming skills with two bands and being a father of three. Legend says, it's him and his identical twin brothers working as a team.

comments powered by Disqus

You're one step away from meeting your best partner in business.

Hire Us