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

# Run Tasks

> Run tasks for single or multiple projects in parallel, define task pipelines, and control execution order across your Nx monorepo.

In a monorepo, you may have dozens or hundreds of projects to manage. Nx provides a task runner that lets you:

* Run multiple targets for multiple projects in parallel
* Define task pipelines that execute in dependency order
* Run tasks only for projects affected by a change
* Speed up execution with [caching](/features/cache-task-results)

## Define tasks

Nx tasks can come from `package.json` scripts, be inferred from tooling configuration files, or be defined explicitly in `project.json`. Nx merges all three sources for each project.

<Tabs>
  <Tab title="project.json">
    ```json theme={null}
    // libs/mylib/project.json
    {
      "root": "libs/mylib",
      "targets": {
        "build": {
          "command": "tsc -p tsconfig.lib.json"
        },
        "test": {
          "executor": "@nx/jest:jest",
          "options": {}
        }
      }
    }
    ```
  </Tab>

  <Tab title="package.json">
    ```json theme={null}
    // libs/mylib/package.json
    {
      "name": "mylib",
      "scripts": {
        "build": "tsc -p tsconfig.lib.json",
        "test": "jest"
      }
    }
    ```
  </Tab>

  <Tab title="Inferred by Nx plugins">
    Nx plugins detect your tooling config files (e.g. `vite.config.ts`, `.eslintrc.json`) and automatically register runnable tasks with caching configured. Target names are set in `nx.json`:

    ```json theme={null}
    // nx.json
    {
      "plugins": [
        {
          "plugin": "@nx/vite/plugin",
          "options": {
            "buildTargetName": "build",
            "testTargetName": "test",
            "serveTargetName": "serve"
          }
        },
        {
          "plugin": "@nx/jest/plugin",
          "options": {
            "targetName": "test"
          }
        }
      ]
    }
    ```
  </Tab>
</Tabs>

## Run tasks

Nx uses the syntax `nx <target> <project>` or the long form `nx run <project>:<target>`.

### Run a single task

```bash theme={null}
npx nx test header
```

### Run tasks for multiple projects

Use `run-many` to run a target across all projects, or a filtered subset:

```bash theme={null}
# Run build for all projects
npx nx run-many -t build

# Run build, lint, and test for all projects
npx nx run-many -t build lint test

# Run tasks only on specific projects
npx nx run-many -t build lint test -p header footer
```

Nx parallelizes execution and runs tasks in the correct order based on project dependencies and your task pipeline configuration.

### Run tasks on projects affected by a PR

```bash theme={null}
npx nx affected -t test
```

See [Affected Builds](/features/affected-builds) for details on how Nx determines which projects to include.

## Define a task pipeline

Many tasks must run in a specific order — for example, building dependencies before the application that uses them. Declare this in `nx.json` using `dependsOn`:

```json theme={null}
// nx.json
{
  "targetDefaults": {
    "build": {
      "dependsOn": ["^build"]
    }
  }
}
```

The `^build` syntax means "run `build` on all projects this project depends on first". If you run `nx build myreactapp` and `myreactapp` depends on `shared-ui` and `feat-products`, Nx will build those two libraries before building the app.

You can define `dependsOn` globally in `nx.json` or per-project in `project.json`.

## Reduce repetitive configuration

Use `targetDefaults` in `nx.json` to set shared configuration that applies to all projects with a matching target name:

```json theme={null}
// nx.json
{
  "targetDefaults": {
    "build": {
      "cache": true,
      "dependsOn": ["^build"],
      "outputs": ["{workspaceRoot}/dist/{projectName}"]
    },
    "test": {
      "cache": true
    }
  }
}
```

## Run root-level tasks

For tasks that apply to the whole codebase rather than a single project, define them at the root level:

<Tabs>
  <Tab title="project.json">
    ```json theme={null}
    // project.json (root)
    {
      "name": "myorg",
      "targets": {
        "docs": {
          "command": "node ./generateDocsSite.js"
        }
      }
    }
    ```
  </Tab>

  <Tab title="package.json">
    ```json theme={null}
    // package.json
    {
      "name": "myorg",
      "scripts": {
        "docs": "nx exec -- node ./generateDocsSite.js"
      },
      "nx": {}
    }
    ```

    <Note>
      The `"nx": {}` property is required to tell Nx this is a root-level project. It can be expanded to specify cache inputs and outputs.
    </Note>
  </Tab>
</Tabs>

Invoke the task with:

```bash theme={null}
npx nx docs
```
