name: design-monorepo-structure description: Use when several related apps and packages (frontend, backend, shared types, UI kit, infra) live or should live in one repository — and you need to decide the folder layout, workspace tooling, dependency rules, ownership, and CI so the repo scales without slow builds or tangled dependencies. source: 'Potvin & Levenberg, "Why Google Stores Billions of Lines of Code in a Single Repository", Communications of the ACM 59(7), 2016; Nx, "Monorepo.tools" and Nx documentation; Vercel, Turborepo documentation; pnpm, "Workspaces" documentation; Bazel documentation, "Visibility"; GitHub Docs, "About code owners"' tags: [project-structure, monorepo, workspaces, build-caching, code-sharing, developer-experience] related: [apply-module-boundary-enforcement, design-repository-layout, apply-changesets, apply-trunk-based-development, design-frontend-project-structure, design-backend-project-structure]
Design Monorepo Structure
Put related apps and libraries in one repository with a clear apps/ vs packages/ split, workspace-aware tooling that builds and tests only what changed, enforced dependency rules, and per-folder ownership.
Why This Is Best Practice
Adopted by:
- Google (a single repository used by around 25,000 engineers, described in CACM 2016), Meta, Microsoft (Rush, Lage), and Uber.
- Vercel (Turborepo), Nrwl (Nx), Babel, React, Next.js, and Jest.
Impact:
- Potvin & Levenberg (2016) cite these benefits of Google's monorepo:
- unified versioning and a single source of truth
- atomic changes across projects
- large-scale refactoring
- easy code sharing
- flexible team boundaries
- Remote caching and affected-only task execution (Nx, Turborepo, Bazel) let CI skip unchanged projects, which keeps pipelines fast as the repo grows.
Why best:
- Polyrepos for tightly related code create version skew, duplicated tooling, and multi-PR changes for one feature.
- A monorepo makes cross-cutting changes atomic and sharing trivial, provided tooling handles scale and boundaries are enforced.
Sources: Potvin, R. & Levenberg, J. (2016), CACM 59(7), 78–87. monorepo.tools (Nrwl). Turborepo, pnpm, and Bazel documentation.
Steps
Decide whether a monorepo fits. Good fit: shared code, coordinated releases, one org owning the code, a desire for atomic cross-project changes. Poor fit: unrelated products, very different access controls, or no capacity to maintain build tooling.
Use the standard layout:
apps/ # deployable things: web, admin, api, mobile, worker web/ api/ packages/ (or libs/) # reusable, non-deployable: ui, config, types, sdk, domain libs ui/ api-client/ eslint-config/ tsconfig/ tools/ # generators, scripts, custom lint rules infra/ # IaC (or a separate repo) docs/ package.json | pnpm-workspace.yaml | nx.json | turbo.json | go.work | Cargo.toml (workspace) CODEOWNERSApps depend on packages. Packages never depend on apps.
Choose tooling by ecosystem and scale:
Need Options JS/TS package linking pnpm workspaces (recommended), npm/yarn workspaces Task orchestration and caching Turborepo (light), Nx (rich: generators, graph, boundaries) Polyglot or very large Bazel, Pants, Buck2 Go / Rust / .NET go.work, Cargo workspaces, solution files plus Directory.Build.propsMake each package self-describing. Each one has its own
package.json/pyproject/Cargo.tomlwith name, scripts (build,test,lint), and explicit dependencies. Use internal names like@acme/ui. Depend on workspace packages withworkspace:*.Share configuration as packages (
@acme/tsconfig,@acme/eslint-config), so apps extend a common base instead of copy-pasting.Enforce dependency rules. Tag projects by type and scope (Nx tags, Bazel
visibility, dependency-cruiser) and fail CI on violations: UI can't import data access, and app A can't import app B. Seeapply-module-boundary-enforcement.Make CI affected-only and cached: run
nx affected -t testorturbo run test --filter=...[origin/main], enable remote caching, and parallelize. Full builds should be the exception, not the norm.Assign ownership with
CODEOWNERSperapps/*andpackages/*folder, so reviews route automatically.Pick a versioning strategy.
- Internal-only packages: no versioning, always latest (trunk).
- Published packages: Changesets for independent semver and changelogs (see
apply-changesets). - Pair either with trunk-based development (see
apply-trunk-based-development).
Use a single version policy for third-party dependencies where possible (one React version, one TypeScript version) to avoid duplicates and drift. Enforce it with syncpack,
pnpmcatalogs, or Nx.
Rules
apps/deploy,packages/are reused, and the dependency arrow only points from apps to packages.- Every project builds and tests independently with standard script names.
- CI runs only what's affected, with caching.
- Boundaries and ownership are encoded in config, not tribal knowledge.
- One version of each third-party dependency unless there's a documented exception.
Common Mistakes
- Monorepo without tooling. Every PR runs everything, CI takes an hour, and teams leave.
- A "shared" package that becomes a dumping ground. Split by purpose (
ui,types,utils-date) and keep each one cohesive. - Relative imports across projects (
../../apps/web/src/...). Always import through package names and public entry points. - Phantom dependencies. Using a dependency hoisted from another package without declaring it. pnpm's strict layout prevents this.
- Mixing unrelated products and access levels. Use separate repos where permissions must differ.
Examples
Full-stack TypeScript:
apps/web(Next.js),apps/api(NestJS)packages/ui(design system),packages/api-contract(zod schemas shared by client and server)packages/tsconfigandpackages/eslint-config- pnpm workspaces plus Turborepo with remote cache. CI runs
turbo run lint test build --filter=...[origin/main].
Go plus web: apps/api (Go module), apps/web (Vite React), packages/proto (protobuf, generated into both). A go.work at the root, pnpm for JS. The Makefile delegates per app.
When NOT to Use
- Independent products with separate teams, release cycles, and access needs. Polyrepos are simpler.
- Open-source packages that need independent contributor communities.
- Organizations without anyone to own build tooling. The cost will show up as slow CI and friction.