Monorepos Explained: What They Are, How They Work & When to Use One
10 min read
Monorepos Explained: What This Is, How This Work, and When You Should Use One
One repository. Multiple applications. Shared code. But is it always the right choice?
Imagine you're building an e-commerce platform.
You have:
A customer-facing website where people browse and buy products.
An admin dashboard where employees manage orders.
A backend API that handles authentication, payments, and business logic.
A shared UI library containing buttons, forms, and other reusable components.
Now comes an important question:
Where should all this code live?
Should every application have its own Git repository? Or should everything live together in one repository?
This is where monorepos come into the picture.
Let's understand them from scratch, without making things unnecessarily complicated.
1. What Exactly Is a Monorepo?
A monorepo (short for monolithic repository) is a software development approach where multiple projects are stored and managed inside a single Git repository.
For example, instead of having three separate repositories:
customer-website/
admin-dashboard/
backend-api/You organize everything into one repository:
ecommerce-platform/
├── apps/
│ ├── web/
│ ├── admin/
│ └── api/
│
├── packages/
│ ├── ui/
│ └── shared/
│
└── package.jsonHere's what those folders represent:
apps/web— the customer-facing website.apps/admin— the admin dashboard.apps/api— the backend application.packages/ui— reusable UI components.packages/shared— common utilities, validation logic, and types.
Everything lives together, but each application can still have its own dependencies, configuration, and deployment process.
The most important thing to understand: A monorepo does not mean one giant application. It means multiple projects managed in one repository.
2. Monorepo vs. Polyrepo: What's the Difference?
The opposite approach is called a polyrepo, also known as a multi-repository architecture.
In a polyrepo, every application or library has its own repository.
Feature | Monorepo | Polyrepo |
|---|---|---|
Git repositories | One | Multiple |
Sharing code | Through internal packages | Usually published packages or external dependencies |
Changes across projects | Can happen in one pull request | Often require multiple PRs |
CI/CD | Shared infrastructure, configurable per app | Usually configured per repository |
Access control | More complex to isolate by project | Easier repository-level isolation |
Tooling | Can become complex as it grows | Simpler for independent projects |
Neither approach is universally better.
The right choice depends on how closely your projects and teams need to work together.
3. Understanding Monorepo Architecture
Think of a monorepo as an office building.
Different companies can occupy different floors, but they may share meeting rooms, security systems, and other facilities.
In the same way, applications inside a monorepo remain separate while sharing useful libraries and development tools.
Consider the accompanying architecture diagram.
The customer website and admin dashboard can reuse the same UI components, while the backend API and other apps can reuse common utilities.
For example:
Shared UI package
// packages/ui/Button.jsx
export function Button({ children }) {
return <button>{children}</button>;
}Both frontend applications can use this component.
Shared utilities package
// packages/shared/currency.js
export function formatPrice(amount) {
return `$${amount.toFixed(2)}`;
}Instead of duplicating this function in multiple applications, you maintain it in one place.
If you fix a bug in the shared code, every application using that code can benefit from the fix after rebuilding or redeploying as needed.
This reduces duplication and helps keep behavior consistent.
However, it also introduces dependencies between projects. We'll discuss why that matters later.
4. How Do You Actually Set Up a Monorepo?
Let's build a very small example using npm workspaces.
You don't need Turborepo, Nx, or any complex tooling to get started.
We'll create a backend application and a shared package, then connect them.
Step 1: Create your project
Open your terminal:
mkdir monorepo-demo
cd monorepo-demo
npm init -yCreate the following folders:
monorepo-demo/
├── apps/
│ └── api/
│
├── packages/
│ └── shared/
│
└── package.jsonStep 2: Configure workspaces
Update the root package.json:
{
"name": "monorepo-demo",
"private": true,
"workspaces": [
"apps/*",
"packages/*"
],
"scripts": {
"start:api": "npm run start -w @demo/api"
}
}Let's break this down.
private: true prevents accidentally publishing the root project to npm.
workspaces tells npm where to find the individual applications and packages.
The start:api script runs the API application's start command from the repository root.
Step 3: Create a shared package
Inside packages/shared, create a package.json:
{
"name": "@demo/shared",
"version": "1.0.0",
"type": "module",
"exports": "./index.js"
}Now create packages/shared/index.js:
export function greet(name) {
return `Hello, ${name}!`;
}This is a simple reusable JavaScript function.
Step 4: Connect the backend
Inside apps/api, create its package.json:
{
"name": "@demo/api",
"private": true,
"type": "module",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"@demo/shared": "1.0.0"
}
}Notice this dependency:
"@demo/shared": "1.0.0"
Instead of manually copying code into the backend, we're declaring a dependency on our shared package.
Now create apps/api/index.js:
import { greet } from "@demo/shared";
console.log(greet("monorepo"));Step 5: Install and run
From the repository root, execute:
npm installnpm recognizes the workspaces and links local packages that match the declared dependencies.
Now run:
npm run start:apiOutput:
Hello, monorepo!Congratulations. You've just created a basic monorepo!
You now have two separate packages living inside one repository, and one can import code directly from the other.
For a real frontend or TypeScript application, you'll also need the appropriate framework configuration, build tools, and TypeScript settings.
But the fundamental idea remains the same.
5. What Happens When Your Monorepo Gets Bigger?
Our example is tiny.
But what happens when your company has ten applications, several shared libraries, and hundreds of developers?
This is where additional tooling becomes useful.
You generally need to solve three important problems.
Problem 1: Running multiple projects
Imagine you want to run your website and backend together.
Without additional tooling, you may need separate terminal commands.
Workspace tools let you target individual projects, while monorepo task runners can coordinate scripts across projects.
Problem 2: Unnecessary builds
Suppose your repository contains:
apps/
├── web/
├── admin/
├── api/
└── mobile/You change a button in the admin dashboard.
Should your CI pipeline rebuild and test every application?
Not necessarily.
Ideally, your tooling identifies what changed and which projects depend on that change.
This is called affected-project detection.
Problem 3: Repeating the same work
Imagine your build takes five minutes.
You change only a README file.
Do you really want to spend another five minutes rebuilding unchanged code?
Task caching allows supported build and test results to be reused when their relevant inputs haven't changed.
That's one reason teams adopt dedicated monorepo tools.
6. Popular Monorepo Tools
You may have heard about these tools:
npm, pnpm, and Yarn Workspaces
These manage packages and dependencies across your workspace. They're often the starting point for a JavaScript or TypeScript monorepo.
Turborepo
Turborepo adds task orchestration and caching on top of your workspace setup.
It is popular for coordinating builds, tests, and development scripts across multiple applications.
Nx
Nx provides project dependency analysis, affected-task execution, caching, and additional development tooling.
It can be particularly useful when your repository contains many interconnected projects.
A simple way to think about them:
A package manager helps your projects find and install dependencies.
A monorepo task runner helps coordinate builds, tests, and other work efficiently.
My suggestion: Start with workspaces. Add Turborepo or Nx when your project actually needs them.
Don't introduce five tools to solve a problem you haven't encountered.
7. How Do Deployments Work in a Monorepo?
One common misconception is that a monorepo requires all applications to deploy together.
That's not true.
You can have one repository and multiple independent deployments.
For example:
Project | Deployment destination |
|---|---|
Customer website | Frontend hosting platform |
Admin dashboard | Separate frontend deployment |
Backend API | Container or cloud server |
Shared UI library | Bundled into consuming frontend apps |
Your CI/CD pipeline can be configured to build, test, and deploy specific applications.
For example:
A developer updates a component in
packages/ui.CI identifies which applications depend on it.
Relevant tests and builds run.
The affected applications can be deployed independently.
Changes in apps/api don't automatically mean the website must be redeployed.
However, shared package changes may affect multiple applications, and your pipeline needs to account for those dependencies.
One repository does not mean one deployment.
And it definitely does not mean your entire system must become a monolith.
8. When Should You Use a Monorepo?
Monorepos are especially valuable when several projects are closely connected.
Scenario A: Multiple applications share code
You're building a website, admin portal, and mobile application that share data models, validation rules, or common libraries.
A monorepo can reduce duplication and make coordination easier.
Scenario B: You frequently change multiple projects together
Imagine adding a new property to a user profile.
You might need to update the backend, shared types, frontend components, and tests.
With a monorepo, those coordinated changes can be made in one pull request and tested together.
Scenario C: You want consistent development standards
Your team wants common linting rules, formatting, testing conventions, and reusable configurations.
A monorepo can make it easier to standardize these practices.
Scenario D: You have one product consisting of several related services
A SaaS platform might have separate billing, authentication, reporting, and frontend projects.
If these systems evolve together and share significant code, a monorepo may simplify development.
9. When Should You NOT Use a Monorepo?
This is just as important as knowing when to use one.
Scenario A: Your projects are completely unrelated
Imagine your company maintains a banking application, a gaming website, and an internal HR tool.
If they share little code and have different teams and development processes, combining them may create unnecessary coupling.
Scenario B: Your team needs strict repository access boundaries
Some organizations require certain teams or contractors to access only specific projects.
A typical monorepo provides repository-wide code access to contributors who can clone it.
Per-project access restrictions may require specialized infrastructure, making separate repositories more practical.
Scenario C: Your repository has grown difficult to manage
Large monorepos can introduce expensive builds, complex dependency graphs, slow local workflows, and challenging CI configurations.
Good tooling can reduce these problems, but it adds maintenance overhead.
Scenario D: You have one simple application
If you're building a straightforward website with no reusable packages or related applications, you probably don't need a monorepo.
A single application repository may be easier to understand and maintain.
You can introduce workspace structure later when there's a concrete need.
10. Common Mistakes Developers Make With Monorepos
Mistake 1: Sharing everything
Not all code belongs in a shared package.
If a piece of logic is useful only to the admin dashboard, keep it there.
Share code when there's a meaningful reason, not simply because you can.
Mistake 2: Creating circular dependencies
Avoid architectures where Package A imports Package B, and Package B imports Package A.
Keep dependencies flowing in a clear direction.
For example, applications can depend on shared libraries, while shared libraries should generally avoid depending on applications.
Mistake 3: Running every test for every small change
This can become expensive as your repository grows.
Use appropriate change detection, caching, and dependency-aware testing once your scale justifies them.
Mistake 4: Ignoring project boundaries
A monorepo should not become a place where any application imports random internal files from any other application.
Define clean package interfaces, clear ownership, and architectural boundaries.
Mistake 5: Choosing a monorepo because it's trendy
A monorepo is an organizational and architectural decision, not a badge of technical sophistication.
If separate repositories make your system simpler, use separate repositories.
11. A Simple Decision Checklist
Before deciding, ask yourself:
Question | Consider a monorepo if... |
|---|---|
Do our projects share significant code? | Yes |
Do changes frequently span multiple projects? | Yes |
Would consistent tooling help us? | Yes |
Can we manage shared ownership and access? | Yes |
Do projects need completely isolated access and workflows? | Consider separate repos |
Is there only one small application? | Start simple |
You don't need every answer to point in the same direction.
The goal is to identify which trade-offs matter most for your team.
12. Best Practices for a Healthy Monorepo
A few habits can make a big difference:
Organize code clearly. Keep applications under
apps/and reusable libraries underpackages/when those conventions fit your stack.Define dependency boundaries. Applications should consume shared packages through well-defined interfaces.
Keep dependencies explicit. Each application should declare the packages it uses.
Maintain independent deployment pipelines. Deploy what needs deploying, not necessarily everything.
Optimize CI as the repository grows. Use caching and affected-project testing when they provide measurable value.
Document ownership. Make it clear who maintains each application and library.
These practices matter more than simply picking a popular monorepo tool.
Final Thoughts: Should You Choose a Monorepo?
Monorepos are powerful, but they're not automatically the best architecture.
They work well when your projects are related, code sharing is common, and developers benefit from making coordinated changes.
They become less attractive when your projects are unrelated, isolation is important, or the shared infrastructure creates more complexity than it solves.
For small teams, simple workspaces might be all you need.
As your product grows, tools like Turborepo and Nx can help manage increasingly complex workflows.
But remember:
A good architecture isn't the one with the most tools. It's the one that makes your system easier to build, understand, and maintain.
And sometimes, that means one repository.
Other times, it means several.
The important thing is knowing why you're choosing either approach.
Further Reading
What does your team prefer: monorepos or separate repositories? And what's been your biggest challenge with either approach?

