What Is a Monorepo? Why Using a Monorepo?

November 18, 2024

☕️ Support Us
Your support will help us to continue to provide quality content.👉 Buy Me a Coffee

You've probably heard people talk about monorepos, especially in frontend development. Tools like Lerna, Nx, and Turborepo are popular choices, and many large companies have dedicated frontend infrastructure teams managing them. But what exactly is a monorepo? Why should you use one? Let's dive in.

What's a Monorepo?

A monorepo is simply a single repository that houses multiple projects. 'Mono' means 'single,' and 'repo' is short for 'repository.' So, instead of having separate repositories for different projects, you put them all under one roof. The opposite of a monorepo is a polyrepo, where each project has its own repository.

Why Bother Using a Monorepo?

Now that we've defined a monorepo, let's talk about the problems it solves. Imagine you have three separate repositories: an app, a documentation site, and a shared utility library. To share the utility library, you'd publish it to npm and have both the app and docs install it.

But if there's a bug in the utility library, you'd have to:

  • Fix the bug in the utility library.
  • Publish a new version to npm.
  • Update the app to use the new version.
  • Update the docs to use the new version.
  • Then deploy both the app and docs.

This process becomes increasingly complex as you add more projects that depend on the utility library.

Enter the Monorepo

With a monorepo, the utility library, app, and docs are all in the same repository. This simplifies things greatly:

  • Fix the bug in the utility library.
  • Both the app and docs are ready to deploy.

Monorepos eliminate the need for version control because the app and docs don't rely on an npm version of the utility library; they depend on the version within the monorepo.

In essence, when using a monorepo, there is no version juggling and no coordination headache. Everything that needs the fix gets it automatically because they're all reading from the same source.

Shared Code, Atomic Changes

The most obvious benefit of monorepos is that sharing code becomes much easier. You can make atomic changes: when you modify the shared code, you can also update the dependent projects in the same commit. This avoids the hassle of coordinating updates across multiple repositories.

Additionally, because everything is in a single repository and relies on a single version, you're less likely to have inconsistencies between different projects. Even infrequently updated projects can stay up-to-date with the latest code. This significantly reduces the burden on developers.

Why Not Just Put Everything in One Repo?

You might be wondering, "Why not just put all the code in a single, giant project?" After all, that would also allow for easy code sharing and a single version of everything.

While simple code colocation can achieve this, it has its downsides. For example, you'd run unnecessary tests. In a simple colocation scenario, even if you make a change that only affects one project, all tests for the entire codebase would run. That's like overhauling your entire car just to change a light bulb.

Moreover, this can lead to a lack of boundaries. If a developer from one team modifies code in another team's project, it could introduce bugs or inconsistencies.

To Sum Up

A monorepo offers the best of both worlds, it allows developers to

  • Enhanced code sharing: Monorepo facilitates seamless sharing of code and libraries across different projects, reducing duplication and improving maintainability.
  • Atomic changes: Changes to shared code and dependent projects can be committed in a single atomic operation, ensuring consistency and reducing the risk of merge conflicts.
  • Reduced inconsistencies: By maintaining a single source of truth, monorepo helps to eliminate inconsistencies between different versions of shared code.

Most major tech companies - Google, Facebook, Microsoft - use monorepos. They've found it's the sweet spot between chaos and rigidity. If you're building software today, especially if you're dealing with multiple related projects, monorepos deserve your attention.

In the end, monorepos aren't just a trend - they're a fundamental shift in how we manage code at scale. And like many good ideas in tech, once you understand them, you wonder how we ever did without them.

☕️ Support Us
Your support will help us to continue to provide quality content.👉 Buy Me a Coffee