Why should you use a monorepo?
In this article, I will show you why you should use a monorepo, the advantages of using a monorepo and why you might consider using a monorepo in your next project and in your company.
Up until recently, I had never even learned about monorepos. I had always used the traditional approach of having one repository per project. The problem that lead me into discovering monorepos was that I had to share code between two projects. I had to copy and paste the code from one project to another, which was a pain in the neck. I really needed a better solution.
I wondered about how Google and Facebook manage millions of lines code. I knew that they cannot have one repository per project or do it manually.
I started looking for a solution and I found out about monorepos. I was really excited about the idea of having all my projects in one repository and these projects can share code with each other. I started looking for a tool that would help me set up a monorepo. I found out about PNPM Workspaces
In this article, I will show you how to set up a monorepo with PNPM Workspaces, Next.JS, Adonis.JS (For backend) and then add Nx for speed.
What is a monorepo?
A monorepo is a repository that contains multiple projects. These projects ca share code with each other. For example, you can have a project for your frontend and another project for your backend. You can also have a project for your shared code. This shared code can be used by both the frontend and the backend.
Not Just "Code colocation"
A monorepo is not just a repository that contains multiple projects. It is also a repository that contains multiple projects that share code with each other and have well defined relationships between them. For example, you can have a project for your frontend and another project for your backend. You can also have a project for your shared code. This shared code can be used by both the frontend and the backend.
Monorepo !== Monolithic
A monorepo is not a monolithic repository. A monolithic repository is a repository that contains all the code for a project. A monorepo is a repository that contains multiple projects. These projects can share code with each other and each project does not have to contain all the code for the project. A monolithic architecture refers to a specific architectural style where an application is built and deployed as a single unit, while a monorepo is a version control approach that can be used to manage multiple projects or applications, including both monolithic and more modular architectures.
Why not use a polyrepo?
The industry has transitioned to the polyrepo approach primarily to empower teams with greater autonomy. With polyrepos, individual teams have the freedom to make decisions regarding the libraries they prefer to use, the timing of their app or library deployments, and the access permissions for contributing to or utilizing their code. This shift allows teams to operate independently, fostering a sense of ownership and enabling them to tailor their workflows to best suit their specific needs and goals.
While autonomy is beneficial, it's important to consider the potential drawbacks of a polyrepo environment. One major concern is the impact on collaboration. Isolation, which enables team autonomy, can hinder effective collaboration among teams. Some potential drawbacks of polyrepos include limited code sharing, increased complexity, significant code duplication and a lack of visibility into the overall health of the codebase, etc...
Why should you use a monorepo?
There are a number of important benefits to using a Monorepo (with a Smart Build System):
- Consistency - All projects in the monorepo use the same tooling and configuration. This makes it easier to maintain and update the projects.
- Code Sharing - Projects in the monorepo can share code with each other. This makes it easier to reuse code and avoid duplication.
- Visibility - All projects in the monorepo are visible to each other. This makes it easier to see what is going on in the monorepo.
- Build and Development Caching - The monorepo can cache build and development artifacts. This makes it easier to build and develop projects in the monorepo.
- Single source of truth - The monorepo is the single source of truth for all projects in the monorepo. This makes it easier to maintain and update the projects.
- A single CI/CD pipeline - The monorepo has a single CI/CD pipeline. This makes it easier to maintain and update the projects.
- Deduplication of node modules - The monorepo can deduplicate node modules. This makes it easier to maintain and update the projects.
- A single package.json - The monorepo has a single package.json. This makes it easier to maintain and update the projects' dependencies and metadata.
- Atomic Commits and Versioning - Monorepos enable atomic commits, where changes to multiple projects can be committed together, ensuring that all projects remain in a consistent state. Additionally, versioning of projects within the monorepo can be done together, allowing for synchronized releases and ensuring compatibility among different components.
How do I make a Monorepo?
Creating a unified repository for a comprehensive JavaScript codebase has become increasingly convenient, thanks to several options available. The simplest approach, requiring no extra dependencies, is to utilize PNPM Workspaces. Additionally, we can explore another alternative solution (NX), to add more features and speed. Check out the full tutorial to learn how to set up a monorepo with PNPM Workspaces, Next.JS, Adonis.JS (For backend) and then add Nx for speed.