Monorepo Advantages and Real Drawbacks
The case for a monorepo is strongest when you have shared code that multiple packages consume, or when API changes need to be coordinated atomically with client changes. The case against is strongest for truly independent products with different teams and release cadences. Evaluate honestly before committing — migrating out of a monorepo is significantly harder than migrating into one.
- Advantage: a change in a shared library updates all consumers automatically
- Advantage: same linting, testing, and CI tools across everything
- Advantage: atomic commits — API change + client update in the same commit
- Drawback: the repo grows fast — need git shallow clone in CI
- Drawback: higher setup complexity upfront
- Drawback: CI becomes slow without affected-only builds
- Advantage: unified dependency management — one lockfile, one version of each dependency
- Advantage: cross-package refactoring in a single PR — no coordination between repos
- Drawback: merge conflicts more likely with many contributors on a single repo
Setting Up pnpm Workspaces: The Essential Steps
pnpm is the right package manager for a monorepo: it is faster than npm and yarn, uses hard links to avoid duplicating package files across workspaces, and has first-class workspace support built in. The catalog feature is particularly valuable — it pins shared dependency versions in one place, eliminating version drift across packages.
- pnpm-workspace.yaml: packages: ['artifacts/*', 'lib/*', 'scripts']
- Catalog: put shared versions in pnpm catalog to unify them
- Root package.json: turbo or workspace-level scripts
- pnpm-lock.yaml: one lockfile for all packages — mandatory
- engines.pnpm in package.json: enforce the same pnpm version across the team
- workspace: protocol: use 'workspace:*' for internal package dependencies
- Hoisting control: pnpm's strict hoisting prevents phantom dependencies
- pnpm patch: patch third-party packages without forking — keeps patches in lockfile
Ideal Project Structure: The Rules That Matter
The most important rule in a monorepo is the dependency direction: artifacts depend on lib, never the reverse. lib packages may depend on other lib packages, but not on artifacts. This acyclic dependency graph enables incremental builds, clear ownership, and simple reasoning about what changing a shared library affects.
- artifacts/: deployable applications (web app, API server, mobile)
- lib/: shared libraries (api-spec, db, utils) — composite TypeScript
- scripts/: shared utility tools
- Rule: artifacts don't import from other artifacts — only from lib/
- Rule: lib/ packages don't import from artifacts/ — one direction only
- Naming convention: @workspace/package-name — consistent prefix for all internal packages
- Per-package tsconfig: each package has its own tsconfig.json extending a shared base
- Per-package README: document what each lib package provides and who consumes it
TypeScript Project References: For Complete Type Safety
TypeScript Project References enable incremental compilation across packages: TypeScript only recompiles packages that have changed, making large monorepos significantly faster to typecheck. The setup requires lib packages to emit declaration files and have composite: true in their tsconfig. This is the primary TypeScript configuration that differs from a single-package setup.
- lib/ packages: composite: true in tsconfig + emit declarations
- Root tsconfig.json: references array for all lib/ packages
- tsc --build: builds libs in correct order with incremental compilation
- artifacts/ reference libs in references: improves editor experience
- Common problem: circular references between libs — draw dependency graph before building
- Declaration maps: declarationMap: true enables go-to-definition to source, not .d.ts
- tsbuildinfo: cached build info speeds up subsequent builds — gitignore it
- typecheck script: pnpm run typecheck:libs && tsc --noEmit in each artifact
Creating Effective Shared Libraries
The right shared libraries are the ones that genuinely eliminate duplication across multiple packages. Resist the temptation to put everything in a shared library — code that is only used by one package belongs in that package. The best shared libraries are the ones with stable, minimal APIs that rarely change.
- api-spec: OpenAPI spec + codegen → hooks and schemas for frontend and backend
- db: Drizzle schema + migration helpers — single source of truth for types
- utils: shared helper functions without heavy dependencies
- i18n: shared translations if you have more than one frontend
- Publishing libraries? Not necessary — pnpm workspace: protocol is sufficient
- Versioning: no need for semver on internal libraries — workspace:* always uses current
- Breaking changes: coordinate in one PR — the main benefit of having a monorepo
- Test coverage: lib packages deserve higher coverage than artifacts — they have more consumers
Efficient CI/CD with a Monorepo
Monorepo CI is where setup pays off or causes pain. Without optimization, CI runs every package's tests on every commit, making CI 10x slower than it needs to be. With Turborepo affected builds and remote caching, CI only runs what actually changed and reuses results from previous runs — reducing CI time from 15 minutes to 3 in our experience.
- Turborepo: build orchestration tool — only runs what changed (affected builds)
- Remote Caching: Turborepo Remote Cache shares cache between CI runs
- Pipeline definition in turbo.json: specifies task dependencies
- Filter by changed packages: pnpm --filter ...[origin/main] run test
- CI time: from 15 minutes to 3 minutes with affected-only + cache
- Parallel task execution: Turborepo runs independent tasks concurrently by default
- Cache invalidation: tasks with external dependencies (DB) must not be cached
- Affected package detection: changes to shared libs correctly invalidate all dependent packages
Common Monorepo Pitfalls and How to Avoid Them
Most monorepo problems come from not enforcing boundaries consistently. A rule that is not automated is a rule that gets broken under deadline pressure. The following pitfalls are ones we have encountered directly and automated solutions for in our setup.
- Phantom dependencies: package uses a dep it didn't declare — pnpm strict mode catches it
- Circular references: lib A imports lib B which imports lib A — draw the graph before building
- Version skew: two packages depend on different versions of React — use catalog to pin
- Monorepo-aware eslint: import/no-cycle catches circular imports at lint time
- Commit scope enforcement: conventional commits with package scope make changelogs meaningful
- IDE performance: large monorepos slow down editors — use per-project tsconfig includes
- Dependency creep: regularly audit which packages depend on each other — keep the graph clean
