Architecture

Monorepo Architecture with Nx and Turborepo: A Practical Guide

9 min read By Suraj Kumar Gorai

Managing multiple frontend applications and shared libraries in separate repositories leads to dependency hell, duplicated tooling, and inconsistent standards. After migrating two enterprise platforms to monorepos, here's the practical guide to making monorepos work with Nx and Turborepo.

When a Monorepo Makes Sense

Consider a monorepo when you have:

  • Multiple applications sharing a component library or design system
  • Shared utility packages used across 3+ applications
  • A team that needs consistent linting, testing, and CI/CD configuration
  • Cross-project refactoring that currently requires coordinated multi-repo PRs

Don't use a monorepo for: completely independent projects with different teams and no shared code.

Nx vs. Turborepo: Choosing the Right Tool

Nx

  • Best for: Enterprise teams wanting a full-featured build system with code generation, dependency graph visualization, and affected-command analysis.
  • Strengths: Rich plugin ecosystem (React, Angular, Node, Nest), nx generate scaffolding, excellent dependency graph visualization with nx graph.
  • Trade-off: Steeper learning curve, more opinions baked in, heavier tooling.

Turborepo

  • Best for: Teams wanting minimal overhead monorepo tooling that enhances existing npm/yarn/pnpm workspaces.
  • Strengths: Zero-config task caching, simple pipeline configuration, works with any framework, minimal learning curve.
  • Trade-off: Fewer built-in features, no code generators, less sophisticated dependency analysis.

Our rule: Nx for new enterprise monorepos where we control the stack. Turborepo for migrating existing multi-repo setups where we need to minimize disruption.

Monorepo Structure

The folder structure that scales:

  • apps/ — Deployable applications (web app, admin panel, mobile app)
  • packages/ — Shared libraries (UI components, utilities, API client, types)
  • tools/ — Build scripts, code generators, custom CLI tools
  • configs/ — Shared ESLint, TypeScript, Prettier, and Jest configurations

Each app and package has its own package.json, tsconfig.json, and test setup. Shared configs are extended, not duplicated.

Task Orchestration & Caching

The killer feature of both Nx and Turborepo: task caching. If the source files for a package haven't changed, the build/test/lint results are served from cache instantly.

  • Local caching: Build a package once, and subsequent builds skip it entirely until source files change. Typical speedup: 3-5x for full builds.
  • Remote caching: Share the cache across CI runners and team members. When a colleague already built the same package, you get their cached result. This is the feature that makes monorepos viable at scale — CI times stay constant regardless of repo size.
  • Affected commands: Only build/test/lint packages affected by a given PR. A change to the UI library triggers builds for the UI library and all apps that depend on it, but not unrelated packages.

Dependency Management

  • Single version policy: Every package in the monorepo uses the same version of shared dependencies (React, TypeScript). This prevents "works in package A but not package B" issues.
  • Package manager: pnpm is our choice for monorepos. Its strict dependency resolution prevents phantom dependencies, and its content-addressable storage is efficient for many packages.
  • Internal package references: Use workspace protocol ("@myorg/ui": "workspace:*") for internal packages. They resolve to the local source, not a published version.

CI/CD for Monorepos

The CI challenge: you don't want every PR to build every app. Our approach:

  • Use affected commands to determine which packages changed in the PR
  • Run builds and tests only for affected packages and their dependents
  • Deploy only the apps that were affected
  • Use remote caching to share build artifacts across CI runs

Result: CI times for a monorepo with 5 apps and 15 packages average 4 minutes, compared to 12+ minutes for a full build.

Considering a monorepo migration? Let's plan the strategy — the transition is significant but the long-term benefits are undeniable.