Skip to main content

Lesson 3 of 8

Monorepo with Turborepo

A monorepo houses multiple packages - your app, design system, shared utilities, API client - in one repository. Turborepo layers intelligent caching and task parallelism on top of npm/pnpm workspaces, making builds and tests dramatically faster.

35 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Turborepo Setup & Configuration

Turborepo Setup & Configuration
# Initialize a new Turborepo monorepo
npx create-turbo@latest my-platform
cd my-platform

# Resulting structure:
# apps/
#   web/          ← Next.js main application
#   admin/        ← React admin panel (Vite)
#   docs/         ← Documentation site (Astro)
# packages/
#   ui/           ← Shared React component library
#   config/       ← Shared ESLint, TypeScript, Tailwind configs
#   api-client/   ← Generated API client (e.g. from OpenAPI)
#   utils/        ← Shared utilities (formatters, validators)

# root package.json - workspace configuration (using pnpm)
{
  "name": "my-platform",
  "private": true,
  "workspaces": ["apps/*", "packages/*"],
  "scripts": {
    "build": "turbo run build",
    "dev": "turbo run dev --parallel",
    "lint": "turbo run lint",
    "test": "turbo run test",
    "type-check": "turbo run type-check"
  },
  "devDependencies": {
    "turbo": "latest"
  }
}

// turbo.json - pipeline configuration
{
  "$schema": "https://turbo.build/schema.json",
  "globalDependencies": ["**/.env.*local"],
  "pipeline": {
    "build": {
      "dependsOn": ["^build"],   // build dependencies first (^)
      "outputs": [".next/**", "dist/**"],  // cache these directories
      "env": ["NODE_ENV", "NEXT_PUBLIC_*", "VITE_*"]
    },
    "dev": {
      "cache": false,            // never cache dev server
      "persistent": true         // long-running task
    },
    "lint": {
      "outputs": []
    },
    "test": {
      "dependsOn": ["build"],
      "outputs": ["coverage/**"],
      "env": ["CI"]
    }
  }
}

// packages/ui/package.json - shared component library
{
  "name": "@my-platform/ui",
  "version": "0.0.0",
  "main": "./src/index.ts",
  "types": "./src/index.ts",
  "scripts": {
    "build": "tsup src/index.ts --format esm,cjs --dts",
    "dev": "tsup src/index.ts --format esm,cjs --dts --watch",
    "lint": "eslint src/"
  },
  "peerDependencies": { "react": "^18", "react-dom": "^18" }
}

// apps/web/package.json - application
{
  "name": "web",
  "dependencies": {
    "@my-platform/ui": "*"    // references local package
  }
}

// pnpm install - links local packages via symlinks
// turbo run build - builds @my-platform/ui before web (dependsOn: ["^build"])
// Second run: restores from cache if nothing changed (~0ms build time)

// packages/ui/src/index.ts - barrel export
export { Button } from './Button';
export { Input } from './Input';
export { Modal } from './Modal';
export type { ButtonProps, InputProps, ModalProps } from './types';

Common Mistakes - Monorepo

  • Not defining `^build` in pipeline - without `dependsOn: ["^build"]`, Turbo might build an app before its dependency package, using stale code
  • Importing cross-package by path - `import { Button } from '../../packages/ui/src/Button'` bypasses the package boundary; always import via package name `@my-platform/ui`
  • Duplicated dependencies - if `react` is installed in both root and a child package with different versions, you'll get React hook errors (`Invalid hook call`); hoist to root and mark as peerDependency in packages
  • Missing build outputs in cache - if you don't declare `outputs` in turbo.json, Turborepo can't restore cached artifacts between runs, losing most of the speed benefit

Tip

Tip

Practice Monorepo with Turborepo in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and flow for Monorepo with Turborepo

Practice Task

Note

Practice Task - (1) Write a working example of Monorepo with Turborepo from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Monorepo with Turborepo is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.

Key Takeaways

  • A monorepo houses multiple packages - your app, design system, shared utilities, API client - in one repository.
  • Not defining `^build` in pipeline - without `dependsOn: ["^build"]`, Turbo might build an app before its dependency package, using stale code
  • Importing cross-package by path - `import { Button } from '../../packages/ui/src/Button'` bypasses the package boundary; always import via package name `@my-platform/ui`
  • Duplicated dependencies - if `react` is installed in both root and a child package with different versions, you'll get React hook errors (`Invalid hook call`); hoist to root and mark as peerDependency in packages

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/8 done