Turborepo
Website: https://turbo.build/repo CLI Tool: turbo Authentication: N/A (Vercel Remote Cache requires auth)
Description
Turborepo is a high-performance build system for JavaScript and TypeScript monorepos. It provides intelligent task scheduling, caching, and parallel execution to make monorepo builds blazingly fast. Developed by Vercel and used by major companies to scale their monorepo workflows.
Commands
Setup
Install Turborepo
npm install turbo --global
npm install turbo --save-dev
pnpm add turbo --global
yarn global add turbo
Install Turborepo globally or locally.
Initialize Turborepo
npx create-turbo@latest
npx create-turbo@latest --example basic
Create new Turborepo monorepo.
Build and Development
Run Tasks
turbo run build
turbo run dev
turbo run test
turbo run lint
Execute tasks across all workspaces.
Run Specific Package
turbo run build --filter=web
turbo run dev --filter=@myorg/ui
turbo run test --filter=./packages/api
Run tasks in specific workspace(s).
Run Multiple Tasks
turbo run build test lint
turbo run build lint --filter=web
Execute multiple tasks in sequence.
Filtering
Filter by Package
turbo run build --filter=web
turbo run build --filter=@myorg/*
turbo run build --filter=./apps/*
Filter by package name or pattern.
Filter by Dependencies
turbo run build --filter=web...
turbo run build --filter=...web
turbo run build --filter=...web...
Include dependencies or dependents.
Filter by Changes
turbo run build --filter=[HEAD^1]
turbo run build --filter=[origin/main]
turbo run test --filter=[HEAD^1]...
Filter by git changes.
Caching
Force Cache
turbo run build --force
Bypass cache and rebuild.
No Cache
turbo run dev --no-cache
Disable caching for this run.
Remote Cache
turbo run build --remote-only
Use only remote cache.
Configuration
Generate Config
turbo gen
turbo gen workspace
turbo gen config
Generate Turborepo configuration or workspace.
Daemon
Daemon Status
turbo daemon status
Check daemon status.
Start Daemon
turbo daemon start
Start Turborepo daemon.
Stop Daemon
turbo daemon stop
Stop Turborepo daemon.
Clean Daemon
turbo daemon clean
Clean daemon artifacts.
Utilities
Prune Monorepo
turbo prune <package>
turbo prune web --docker
Create subset of monorepo for deployment.
Link Remote Cache
turbo link
Connect to Vercel Remote Cache.
Unlink Remote Cache
turbo unlink
Disconnect from Remote Cache.
Login
turbo login
Authenticate with Vercel for Remote Cache.
Logout
turbo logout
Remove Vercel authentication.
Information
Version
turbo --version
turbo -v
Show Turborepo version.
Help
turbo --help
turbo run --help
Display help information.
Dry Run
Dry Run
turbo run build --dry
turbo run build --dry=json
Show what would be executed without running.
Graph
Generate Graph
turbo run build --graph
turbo run build --graph=graph.png
turbo run build --graph=graph.svg
Generate task dependency graph.
Examples
Basic Monorepo Setup
// turbo.json
{
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"build": {
"dependsOn": ["^build"],
"outputs": ["dist/**", ".next/**"]
},
"dev": {
"cache": false,
"persistent": true
},
"test": {
"dependsOn": ["build"]
},
"lint": {}
}
}
Common Workflows
# Install dependencies (use package manager)
pnpm install
# Build all packages
turbo run build
# Run all packages in dev mode
turbo run dev
# Test everything
turbo run test
# Lint all code
turbo run lint
# Build and test
turbo run build test
Filtered Execution
# Build specific app
turbo run build --filter=web
# Build app and dependencies
turbo run build --filter=web...
# Build all packages that depend on ui
turbo run build --filter=...@myorg/ui
# Build changed packages
turbo run build --filter=[origin/main]
# Test only changed packages
turbo run test --filter=[HEAD^1]
Parallel Execution
# Run tasks in parallel (default)
turbo run build
# Limit concurrency
turbo run build --concurrency=2
# Serial execution
turbo run build --concurrency=1
# Continue on error
turbo run test --continue
Caching Strategies
# Normal run (use cache)
turbo run build
# Force rebuild (ignore cache)
turbo run build --force
# Disable cache completely
turbo run dev --no-cache
# Use only remote cache
turbo run build --remote-only
Remote Caching Setup
# Login to Vercel
turbo login
# Link repository
turbo link
# Build with remote cache
turbo run build
# Check cache hits
turbo run build --summarize
Pipeline Configuration
// turbo.json - Advanced
{
"pipeline": {
"build": {
"dependsOn": ["^build"],
"outputs": ["dist/**", "build/**"],
"env": ["NODE_ENV"],
"cache": true
},
"test": {
"dependsOn": ["build"],
"inputs": ["src/**", "test/**"],
"outputs": ["coverage/**"]
},
"lint": {
"outputs": []
},
"dev": {
"cache": false,
"persistent": true
},
"deploy": {
"dependsOn": ["build", "test"],
"cache": false
}
}
}
Docker Deployment
# Prune for web app
turbo prune web --docker
# Creates:
# - out/json/ (package.json files)
# - out/full/ (source code)
# Use in Dockerfile
FROM node:18 AS base
WORKDIR /app
# Install dependencies
COPY out/json/ .
RUN npm install
# Copy source
COPY out/full/ .
# Build
RUN turbo run build --filter=web
Git-Based Filtering
# Changed since main
turbo run build --filter=[origin/main]
# Changed in last commit
turbo run test --filter=[HEAD^1]
# Changed between commits
turbo run lint --filter=[abc123...def456]
# Only changed packages
turbo run build --filter=[HEAD^1] --dry
Environment Variables
// turbo.json
{
"pipeline": {
"build": {
"env": ["DATABASE_URL", "API_KEY"],
"passThroughEnv": ["CI", "NODE_ENV"]
}
}
}
Task Dependencies
// turbo.json
{
"pipeline": {
"build": {
"dependsOn": ["^build"] // Depends on dependencies' build
},
"test": {
"dependsOn": ["build"] // Depends on own build
},
"deploy": {
"dependsOn": ["build", "test", "lint"]
}
}
}
Multiple Package Managers
# npm
npm install
turbo run build
# pnpm (recommended)
pnpm install
turbo run build
# yarn
yarn install
turbo run build
CI/CD Integration
# GitHub Actions
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- run: npm install
- run: turbo run build test lint
env:
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: ${{ vars.TURBO_TEAM }}
Workspace Configuration
// package.json (root)
{
"name": "my-monorepo",
"private": true,
"workspaces": [
"apps/*",
"packages/*"
],
"scripts": {
"build": "turbo run build",
"dev": "turbo run dev",
"test": "turbo run test",
"lint": "turbo run lint"
},
"devDependencies": {
"turbo": "latest"
}
}
Output Summary
# Show task summary
turbo run build --summarize
# Show task summary with JSON
turbo run build --summarize=true --output-logs=errors-only
# Show only errors
turbo run test --output-logs=errors-only
Notes
- Monorepo: Supports npm, pnpm, yarn workspaces
- Caching: Local and remote cache for faster builds
- Incremental: Only rebuilds what changed
- Parallel: Runs tasks in parallel automatically
- Pipeline: Define task dependencies in turbo.json
- Filtering: Powerful package and git-based filtering
- Remote Cache: Share cache across team and CI
- Prune: Extract subset for deployment
- Daemon: Background process for faster builds
- Outputs: Cache build artifacts
- Inputs: Determine cache invalidation
- Environment: Hash env vars for cache
- Dependencies:
^for dependencies, normal for own - Persistent Tasks: Long-running tasks like dev servers
- Graph: Visualize task dependencies
- Dry Run: Preview execution without running
- Concurrency: Control parallel task execution
- Continue: Keep running on errors
- Force: Bypass cache
- Performance: 10x faster builds typical
- Vercel: Free remote cache on Vercel
- GitHub Actions: Native integration
- Docker: Optimized Docker builds
- TypeScript: Full TypeScript support
- Best Practices:
- Use pnpm for best performance
- Define clear pipeline dependencies
- Enable remote cache for teams
- Use filtering in CI for speed
- Cache build outputs properly
- Set environment variables explicitly
- Use prune for Docker builds
- Monitor cache hit rates
- Keep turbo.json simple
- Use workspaces effectively
- Version lock turbo
- Test locally before CI
- Use git filtering in CI
- Clean cache when needed
Comments (0)
Add a Comment
No comments yet. Be the first to comment!