What is a monorepo?
- React Native course
- Expert
- Lesson 19/47
- 5 min
Learn the monorepo concept.
What is a monorepo?
A monorepo keeps multiple packages or apps in a single Git repository. Each package has its own package.json; shared dependencies are managed from the root. It is contrasted with polyrepo.
Pros: easy code sharing, single-commit changes, synced versions. Cons: the repo grows, builds get complex. This is why tools like Turborepo exist.
Monorepo structure
A typical monorepo root contains: apps/ (apps), packages/ (shared libraries), package.json (root) and turbo.json (build settings). Each sub-package has its own package.json.
The root package.json declares sub-packages with the workspaces setting. This way npm install sets up all dependencies in one command; conflicting versions are resolved in one place.
Practice Add the workspaces setting
Add a workspaces field to a root package.json. It should include apps/* and packages/* directories.
Starter code
{
"name": "my-monorepo",
"private": true
// Add the workspaces field here
}Sample solution
{
"name": "my-monorepo",
"private": true,
"workspaces": ["apps/*", "packages/*"]
}Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does a monorepo keep in one place?
- Only docs
- One package in many repos
- Many packages in one repo Correct answer
- Only tests
Explanation: A monorepo keeps many packages in one repo.
2 Write the apps folder name
At the monorepo root, apps usually live in the ____/ folder.
At the monorepo root, apps usually live in the apps/ folder.
Explanation: The apps/ folder holds apps.
3 Match the monorepo pieces
| Term | Meaning |
|---|---|
apps/ |
Apps |
packages/ |
Shared library |
package.json |
Root deps |
turbo.json |
Build config |