Should You Use a Monorepo?
One of the most frequently asked questions regarding project structure is whether to use a monorepo. Before jumping straight to the answer, let's start by defining what a monorepo is.
What is a Monorepo?
A monorepo is a single repository that contains multiple packages within it, with each package able to include multiple libraries or applications.
The idea behind a monorepo is simple: you co-locate all the necessary code together, including your application and any custom packages you rely on, such as a design system. This approach makes versioning and maintenance easier, but it also means that the repository can become fairly large.
By default, the TurboRepo monorepos used in this course contain two applications and three packages.
When to Use a Monorepo
Here are some scenarios where using a monorepo can be beneficial:
Multiple Applications with Shared Code: If you have multiple applications and there's potential to share code or standards files, a monorepo is a good choice. This is the default setup of the TurboRepo off-the-shelf starter.
Single Application with Multiple Deployment Options: When you have a single application with various deployment options, such as a web version, mobile version, and desktop version (e.g., using Electron), a monorepo allows you to share a common core application and components.
Separately Deployable Design System Components: If you have design system components that you want to deploy separately from the applications, a monorepo enables you to do so. You can deploy each package or application individually within a monorepo.
Packages with Self-Contained Business Logic: When you have packages containing self-contained business logic, such as API wrappers or business logic that you want to keep separate from the UI, a monorepo is useful. Your application can use these packages to make calls, and you can publish the API wrappers or logic as separate packages.
Monorepo Options
Several monorepo options are available. Package managers like PMPM, Yarn, and NPM have built-in workspace functionality, which forms the core of monorepos. This means you can leverage this functionality without installing anything new.
Monorepo packages like TurboRepo, NX, or MoonRepo provide additional tooling on top of the workspace functionality, making it easier to use. They also offer performance upgrades and caching, which are beneficial for CI/CD pipelines.
Alternatives to Monorepos
If you have an application that stands alone, you might not need a monorepo or any kind of NPM linking. NPM linking is an alternative approach to monorepos.
With NPM linking, you have multiple repositories, each with its own NPM package. When you want to develop on an application and a specific package simultaneously, you use NPM, PMPM, or Yarn linking to create a live link during development.
Once you finish development, you push your changes to both the NPM module repository and the application module repository. This approach can be less convenient compared to using a monorepo.
Migrating to a Monorepo
Migrating to a monorepo doesn't have to be a decision made immediately. You can easily migrate a single application or package into a monorepo structure.
To migrate, it's recommended to start with a new Git repository, port the existing code into the monorepo structure, and continue development from there. You can then deactivate the older repository.
By understanding the scenarios where monorepos shine and the options available, you can make an informed decision on whether to adopt a monorepo structure for your projects.