Overview
Turborepo is a high-performance build system for JavaScript/TypeScript monorepos that intelligently caches task outputs, parallelizes execution across CPU cores, and ensures teams only rebuild what has changed. It integrates with npm, yarn, and pnpm workspaces.
Instructions
- Install
turboas a root devDependency (npm install turbo --save-dev, or the pnpm/yarn/bun equivalent) and run it through the package manager. Turborepo 2.x needs apackageManagerfield in the rootpackage.jsonand a workspace definition (workspaces, orpnpm-workspace.yaml). - Define tasks under the
taskskey ofturbo.json(it was calledpipelinein 1.x;npx @turbo/codemod migrateupgrades old repos). UsedependsOnfor ordering, the^prefix for the same task in dependencies ("dependsOn": ["^build"]), andoutputsfor cacheable artifacts (["dist/**", ".next/**", "!.next/cache/**"]). - Run tasks with
turbo run build lint(orturbo build). Add--ui=streamfor plain log output in CI. Mark long-running tasks"cache": false, "persistent": true. - Caching: the hash covers the package's files, its dependencies' hashes, the task's
inputs(default: all git-tracked files in the package) and declared environment variables. Declare variables in a task'senv(or rootglobalEnv). Taskenvmode isstrictby default: undeclared variables are not passed to the task, so list runtime-only secrets inpassThroughEnvrather thanenvif they should not affect the hash. - Filtering:
--filter=@shop/webfor one package,--filter=@shop/web...for it plus its dependencies,--filter='...[origin/main]'for packages changed since a git ref, or--affectedfor packages changed againstmain(and their dependents).turbo lslists packages;turbo run build --graph=graph.mermaidwrites the task graph;--dry=jsonshows what would run and the hashes. - Docker:
turbo prune @shop/web --dockerwritesout/json(package.json files and pruned lockfile, for the install layer),out/full(sources) and the lockfile. Copyout/json/first, install, then copyout/full/and build. The--scopeflag no longer exists; pass the package name as an argument. Add a.dockerignorewithnode_modules. - Remote Cache:
npx turbo loginthennpx turbo linkfor the Vercel Remote Cache (free on all plans). In CI, setTURBO_TOKENandTURBO_TEAMinstead. Set"remoteCache": {"signature": true}plusTURBO_REMOTE_CACHE_SIGNATURE_KEYto sign artifacts. Self-hosted caches implement the documented Remote Cache API and are selected withTURBO_API. - Shared packages: create focused internal packages (
@repo/ui,@repo/db) with sharedtsconfigand ESLint config packages;turbo generatescaffolds a new app or package.
Examples
Example 1: Set up a monorepo with a web app and a shared UI package
User request: "Initialize a monorepo with a Next.js app and shared UI library, with caching"
Root package.json and turbo.json:
{
"name": "shop",
"private": true,
"packageManager": "pnpm@10.18.0",
"devDependencies": { "turbo": "^2.11.10" }
}
{
"$schema": "https://turborepo.dev/schema.json",
"tasks": {
"build": {
"dependsOn": ["^build"],
"outputs": [".next/**", "!.next/cache/**", "dist/**"],
"env": ["NEXT_PUBLIC_API_URL"]
},
"lint": {},
"dev": { "cache": false, "persistent": true }
}
}
With pnpm-workspace.yaml listing apps/* and packages/* (apps/web named @shop/web, packages/ui named @shop/ui), run pnpm turbo run build. The first run prints cache miss, executing; an immediate second run prints cache hit, replaying logs for both packages and ends with FULL TURBO. Changing NEXT_PUBLIC_API_URL produces a miss again because it is declared in env.
Example 2: Build one service into a small Docker image
User request: "Create a Dockerfile for deploying @shop/api from our Turborepo monorepo"
$ npx turbo prune @shop/api --docker
Generating pruned monorepo for @shop/api in ./out
- Added @shop/api
- Added @shop/db
FROM node:24-slim AS base
RUN corepack enable
WORKDIR /app
FROM base AS pruner
COPY . .
RUN npx turbo@2 prune @shop/api --docker
FROM base AS build
COPY --from=pruner /app/out/json/ .
RUN pnpm install --frozen-lockfile
COPY --from=pruner /app/out/full/ .
RUN pnpm turbo run build --filter=@shop/api
CMD ["node", "apps/api/dist/server.js"]
The install layer is reused until a package.json or the lockfile changes, and the image holds only @shop/api and @shop/db. Pass TURBO_TOKEN and TURBO_TEAM as build secrets if the build should use the Remote Cache.
Guidelines
- Always declare
outputsfor tasks that produce files; a task with nooutputscaches only its logs. Exclude framework caches such as!.next/cache/**. - Declare every environment variable that changes build output in
envorglobalEnv; forgetting one gives stale cache hits, andstrictmode hides undeclared variables from the task. - Use
^buildindependsOnwhen a task consumes dependencies' build output; use a plain name ("dependsOn": ["build"]) for same-package ordering. - Run
turbofrom the repository root, and commit the lockfile: it is part of the hash andturbo pruneneeds it. - Never copy the whole monorepo into an image; use
turbo prune --docker. - Keep tokens (
TURBO_TOKEN) in CI secrets, not inturbo.jsonor the repository. Enable signature verification when the cache is shared with less trusted contributors. - Turborepo orchestrates scripts; it does not replace a bundler or a package manager. For a single-package repository it adds little.