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.

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

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.
// libs/mylib/project.json
{
  "root": "libs/mylib",
  "targets": {
    "build": {
      "command": "tsc -p tsconfig.lib.json"
    },
    "test": {
      "executor": "@nx/jest:jest",
      "options": {}
    }
  }
}

Run tasks

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

Run a single task

npx nx test header

Run tasks for multiple projects

Use run-many to run a target across all projects, or a filtered subset:
# 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

npx nx affected -t test
See 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:
// 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:
// 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:
// project.json (root)
{
  "name": "myorg",
  "targets": {
    "docs": {
      "command": "node ./generateDocsSite.js"
    }
  }
}
Invoke the task with:
npx nx docs