Run Tasks
In a monorepo setup, you don't just run tasks for a single project; you might have hundreds to manage. To help with this, Nx provides a powerful task runner that allows you to:
- easily run multiple targets for multiple projects in parallel
- define task pipelines to run tasks in the correct order
- only run tasks for projects affected by a given change
- speed up task execution with caching
Defining Tasks
Nx tasks can be created from existing package.json
scripts, inferred from tooling configuration files, or defined in a project.json
file. Nx combines these three sources to determine the tasks for a particular project.
1{
2 "name": "mylib",
3 "scripts": {
4 "build": "tsc -p tsconfig.lib.json",
5 "test": "jest"
6 }
7}
8
The project configuration docs has the details for all the available configuration options.
Running Tasks
Nx uses the following syntax:
Run a Single Task
To run the test
task for the header
project run this command:
โฏ
npx nx test header
Run Tasks for Multiple Projects
You can use the run-many
command to run a task for multiple projects. Here are a couple of examples.
Run the build
task for all projects in the repo:
โฏ
npx nx run-many -t build
Run the build
, lint
and test
task for all projects in the repo:
โฏ
npx nx run-many -t build lint test
Run the build
, lint
, and test
tasks only on the header
and footer
projects:
โฏ
npx nx run-many -t build lint test -p header footer
Nx parallelizes these tasks, ensuring they run in the correct order based on their dependencies and task pipeline configuration. You can also control how many tasks run in parallel at once.
Learn more about the run-many command.
Run Tasks on Projects Affected by a PR
You can also run a command for all the projects affected by your PR like this:
โฏ
npx nx affected -t test
Learn more about the affected command here.
Defining a Task Pipeline
It is pretty common to have dependencies between tasks, requiring one task to be run before another. For example, you might want to run the build
target on the header
project before running the build
target on the app
project.
Nx can automatically detect the dependencies between projects (see project graph).
However, you need to specify for which targets this ordering is important. In the following example we are telling Nx that before running the build
target it needs to run the build
target on all the projects the current project depends on:
1{
2 ...
3 "targetDefaults": {
4 "build": {
5 "dependsOn": ["^build"]
6 }
7 }
8}
9
This means that if we run nx build myreactapp
, Nx will first execute build
on modules-shared-ui
and modules-products
before running build
on myreactapp
.
You can define these task dependencies globally for your workspace in nx.json
or individually in each project's project.json
file.
Learn more about:
Reduce repetitive configuration
Learn more about leveraging targetDefaults
to reduce repetitive configuration in the dedicated recipe.
Run Root-Level Tasks
Sometimes, you need tasks that apply to the entire codebase rather than a single project. To still benefit from caching, you can run these tasks through the "Nx pipeline". Define them in the root-level package.json
or project.json
as follows:
1{
2 "name": "myorg",
3 "scripts": {
4 "docs": "node ./generateDocsSite.js"
5 },
6 "nx": {}
7}
8
Note the
nx: {}
property on thepackage.json
. This is necessary to inform Nx about this root-level project. The property can also be expanded to specify cache inputs and outputs.
If you want Nx to cache the task, but prefer to use npm (or pnpm/yarn) to run the script (i.e. npm run docs
) you can use the nx exec command:
1{
2 "name": "myorg",
3 "scripts": {
4 "docs": "nx exec -- node ./generateDocsSite.js"
5 },
6 "nx": {}
7}
8
To invoke the task, use:
โฏ
npx nx docs
Learn more about root-level tasks on our dedicated recipe page.