How to Configure Turborepo Pipeline Caching
Describe task dependencies and cache inputs and outputs so a monorepo build skips work that has not changed.
On this page
Introduction
In a monorepo the build of one package often depends on the build of another, and a naive pipeline either builds everything every time or builds in the wrong order. Turborepo models the pipeline as tasks with declared dependencies, runs them in dependency order and in parallel where possible, and caches each task’s outputs keyed by its inputs. The result is that a change to one package rebuilds only that package and the ones that depend on it, and unchanged packages are restored from cache. This article covers the pipeline declaration, cache inputs and outputs, remote caching, and how to debug a cache that misses when it should hit.
What a Task Pipeline Is
A task pipeline declares, for each task name, what it depends on and how it relates to the same task in other packages. Building a package depends on building the packages it depends on; testing a package depends on its build. Declaring these relationships lets the runner order the work correctly and parallelize independent tasks, instead of running a fixed sequence or trusting a hand-maintained order.
Without a pipeline, a runner does not know that app depends on lib, so it may build app before lib or rebuild everything serially. The pipeline is the missing dependency information, expressed once and used to schedule every run.
Declaring Dependencies
Dependencies are declared with a small set of expressions: build the package’s own dependencies first, build the dependencies of dependencies, depend on the same task in a dependency, or depend on a related task. The most common are building dependencies first and testing after building.
{
"$schema": "https://turbo.build/schema.json",
"tasks": {
"build": {
"dependsOn": ["^build"], // build dependencies before this package
"outputs": ["dist/**", ".next/**", "!.next/cache/**"]
},
"test": {
"dependsOn": ["build"], // test after building this package
"outputs": []
},
"lint": {
"outputs": []
},
"deploy": {
"dependsOn": ["build", "test", "lint"],
"outputs": []
}
}
}
Cache Inputs and Outputs
A task is cacheable if the runner can hash its inputs and restore its outputs. Inputs default to the package’s source files, its dependencies’ hashes, and the relevant lockfile and environment; outputs are the files the task writes, declared so they can be stored and restored. A task with no declared outputs is treated as uncacheable unless the runner is configured to cache its logs anyway.
Declaring outputs precisely matters for correctness and size. Outputs that are not declared are not restored, so a downstream task may miss files it needs; outputs that are too broad bloat the cache. Exclude directories that are themselves caches, such as a framework’s internal cache directory, because caching a cache wastes space and can store stale state.
Remote Caching for CI
Local caching helps a developer but not a fresh CI machine. Remote caching uploads a task’s outputs to a shared store keyed by the same hash, so a CI machine can restore an artifact another machine produced instead of recomputing it. Configure the token and team, and enable it in CI; the hash is the same, so a locally cached task also hits remotely if the inputs match.
# Locally: authenticate and link the repo to a remote cache
npx turbo login && npx turbo link
# CI: enable the remote cache via environment variables
export TURBO_TOKEN="$CI_CACHE_TOKEN"
export TURBO_TEAM="$CI_TEAM"
npx turbo run build test --cache-dir=.turbo/cache
# See exactly what ran, what was cached, and how much time was saved
npx turbo run build --dry=json | jq '.tasks[] | {taskId, cache: .cache}'
npx turbo run build --summarise
Correctness of Cache Keys
The key must cover every input that changes the output. Turborepo hashes the package’s files and its dependencies’ hashes, plus the environment variables and files you declare as global dependencies. An environment variable that changes the build but is not declared produces a wrong cache hit; declare it in the task or globally. A file outside the package, such as a shared config, must be declared as a global dependency or a change to it will not invalidate the cache.
Workspace dependency hashes are what make the pipeline correct: a change to a library changes the hash of the app that depends on it, so the app rebuilds. If that propagation is wrong, changing a library would not rebuild its consumers, and stale output would ship. Test this by changing a library and asserting the dependent package rebuilds.
Debugging Cache Misses
When a task should hit the cache but misses, ask the runner why. Both Turborepo and Nx can explain the hash inputs; the answer is usually an undeclared environment variable, a file outside the package that changed, or a timestamp embedded in the output. Read the run summary to see which task ran and which was restored, then compare the inputs between the two runs.
A subtle cause is non-deterministic output: a build that embeds the current time or an absolute path produces a different artifact each run, so a downstream hash differs even though the source did not. Make the output deterministic, or exclude the volatile field from the hashed outputs. Non-determinism defeats caching whether it is local or remote.
Conclusion
Turborepo caching turns a monorepo build into run-what-changed by declaring task dependencies, hashing each task’s inputs, and restoring its outputs from cache. Declare outputs precisely, exclude nested caches, enable remote caching so fresh CI machines start warm, and declare every environment variable and shared file that affects a task. Verify that a change to a library invalidates its consumers, and eliminate non-deterministic output that makes every hash differ. Then the pipeline spends its time on the packages that changed, not the whole repository.
Sources: Turborepo: configuring turbo.json, Turborepo: caching, Nx: task pipeline.
FAQ
- What does dependsOn "^build" mean?
- It means build the dependencies of this package before building this package. The caret refers to the packages this one depends on, so the runner builds the graph from the leaves up. Without it, a package may build before the library it imports.
- Why would a task miss the cache when nothing changed?
- Usually an undeclared input: an environment variable, a shared file outside the package, or non-deterministic output such as an embedded timestamp. Read the runner hash explanation and compare inputs between runs to find the volatile input.
Report a problem with this articleOpens a pre-filled issue on GitHub.