Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/nrwl/nx/llms.txt

Use this file to discover all available pages before exploring further.

Rebuilding and retesting the same code repeatedly is costly. Nx provides a computation caching system that ensures code is never rebuilt twice. It:
  • Drastically speeds up local and CI task execution
  • Saves money on CI/CD costs by skipping tasks that haven’t changed
  • Restores both terminal output and output files (e.g. your dist directory)

Enable caching for tasks

Add "cache": true to any target in targetDefaults inside nx.json:
// nx.json
{
  "targetDefaults": {
    "build": {
      "cache": true
    },
    "test": {
      "cache": true
    }
  }
}
Cacheable tasks must be side-effect free — given the same inputs, they must always produce the same outputs. For example, e2e tests that hit a live backend API cannot be safely cached because the backend state can vary between runs.

View cache hits

When a cached result is replayed, Nx shows [local cache] next to the task in the terminal output:
nx run header:build  [local cache]
The terminal output from the original run is replayed, and any output files are restored to their original locations — all without re-executing the task.

Clear the cache

To clear the local cache and force all tasks to re-execute:
npx nx reset
The local cache is stored at .nx/cache by default.

Fine-tune caching with inputs and outputs

Nx caches tasks based on a hash of their inputs. You can control exactly what’s included:
  • Inputs — files, environment variables, and other values included in the hash
  • Outputs — folders where task artifacts are written
// nx.json
{
  "targetDefaults": {
    "build": {
      "inputs": ["{projectRoot}/**/*", "!{projectRoot}/**/*.md"],
      "outputs": ["{workspaceRoot}/dist/{projectName}"]
    }
  }
}
In the example above, *.md files are excluded from the input hash so that changing a README does not invalidate the build cache.
You only need to define output locations if they differ from the standard dist or build directories, which Nx recognizes automatically.

Configure caching automatically with plugins

When you use Nx plugins, caching is often configured automatically. For example, adding @nx/vite detects your vite.config.ts and configures the build target’s cache settings — including inputs and outputs — without any manual configuration:
npx nx add @nx/vite
To inspect the inferred cache configuration for a project:
nx show project <project-name> --web

Enable remote caching

Local caching only helps on a single machine. The largest gains come from remote caching, which shares the cache across all developers and CI runs. Connect your workspace to Nx Cloud to enable remote caching (Nx Replay):
npx nx@latest connect

30–70% faster CI

Teams report significantly faster CI pipelines after enabling remote caching, as tasks completed on one machine are replayed instantly on others.

Secure by default

Cache entries are immutable and access is controlled via tokens. End-to-end encryption is available for organizations with strict compliance requirements.

Troubleshoot caching

Confirm "cache": true is set in targetDefaults for the relevant target in nx.json. If you’re using plugins, run nx show project <name> --web to check inferred configuration.
Your inputs or outputs may be misconfigured. If a task reads files not listed in inputs, or writes outside its declared outputs, cache restores will be incomplete. See the debug cache misses guide for details.
Pass --skip-nx-cache to bypass caching for a specific invocation:
npx nx build myapp --skip-nx-cache
The local cache defaults to .nx/cache. You can override this with the NX_CACHE_DIRECTORY environment variable or via nx.json configuration.