> ## 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.

# Cache Task Results

> Avoid rebuilding the same code twice with Nx's computation cache. Cache task results locally and share them across your team with remote caching.

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`:

```json theme={null}
// nx.json
{
  "targetDefaults": {
    "build": {
      "cache": true
    },
    "test": {
      "cache": true
    }
  }
}
```

<Note>
  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.
</Note>

## 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:

```bash theme={null}
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

<Tabs>
  <Tab title="Globally (nx.json)">
    ```json theme={null}
    // nx.json
    {
      "targetDefaults": {
        "build": {
          "inputs": ["{projectRoot}/**/*", "!{projectRoot}/**/*.md"],
          "outputs": ["{workspaceRoot}/dist/{projectName}"]
        }
      }
    }
    ```
  </Tab>

  <Tab title="Per project (project.json)">
    ```json theme={null}
    // packages/some-project/project.json
    {
      "name": "some-project",
      "targets": {
        "build": {
          "inputs": ["!{projectRoot}/**/*.md"],
          "outputs": ["{workspaceRoot}/dist/apps/some-project"]
        }
      }
    }
    ```
  </Tab>

  <Tab title="Per project (package.json)">
    ```json theme={null}
    // packages/some-project/package.json
    {
      "name": "some-project",
      "nx": {
        "targets": {
          "build": {
            "inputs": ["!{projectRoot}/**/*.md"],
            "outputs": ["{workspaceRoot}/dist/apps/some-project"]
          }
        }
      }
    }
    ```
  </Tab>
</Tabs>

In the example above, `*.md` files are excluded from the input hash so that changing a README does not invalidate the build cache.

<Tip>
  You only need to define output locations if they differ from the standard `dist` or `build` directories, which Nx recognizes automatically.
</Tip>

## 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:

```bash theme={null}
npx nx add @nx/vite
```

To inspect the inferred cache configuration for a project:

```bash theme={null}
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):

```bash theme={null}
npx nx@latest connect
```

<CardGroup cols={2}>
  <Card title="30–70% faster CI" icon="gauge">
    Teams report significantly faster CI pipelines after enabling remote caching, as tasks completed on one machine are replayed instantly on others.
  </Card>

  <Card title="Secure by default" icon="lock">
    Cache entries are immutable and access is controlled via tokens. End-to-end encryption is available for organizations with strict compliance requirements.
  </Card>
</CardGroup>

## Troubleshoot caching

<AccordionGroup>
  <Accordion title="Tasks are not being cached">
    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.
  </Accordion>

  <Accordion title="Cache hits are not being restored">
    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](/concepts/how-caching-works) for details.
  </Accordion>

  <Accordion title="Skip the cache for a single run">
    Pass `--skip-nx-cache` to bypass caching for a specific invocation:

    ```bash theme={null}
    npx nx build myapp --skip-nx-cache
    ```
  </Accordion>

  <Accordion title="Change the cache location">
    The local cache defaults to `.nx/cache`. You can override this with the `NX_CACHE_DIRECTORY` environment variable or via `nx.json` configuration.
  </Accordion>
</AccordionGroup>
