Run Tasks

Monorepos can have hundreds or even thousands of projects, so being able to run actions against all (or some) of them is a key feature of a tool like Nx.

Definitions

  • Command - anything the developer types into the terminal (e.g., nx run header:build).
  • Target - the name of an action taken on a project (e.g., build)
  • Task - an invocation of a target on a specific project (e.g., header:build).

Define Tasks

For these examples, we'll imagine a repo that has three projects: myapp, header and footer. myapp is a deployable app and uses the header and footer libraries.

Each project has the test and build targets defined. Tasks can be defined as npm scripts in a project's package.json file or as targets in a project.json file:

package.json
{ "scripts": { "build": "webpack -c webpack.conf.js", "test": "jest --coverage" } }

Run a Single Task

To run the test target on the header project run this command:

npx nx test header

Run Everything

To run the build target for all projects in the repo, run:

npx nx run-many --target=build

This will build the projects in the right order: footer and header and then myapp.

Terminal

~/workspace

npx nx run-many --target=build

✔ nx run header:build (501ms) ✔ nx run footer:build (503ms) ✔ nx run myapp:build (670ms) ————————————————————————————————————————————————————————————————————————————— > NX Successfully ran target build for 3 projects (1s)

Note that Nx doesn't care what each of the build scripts does. The name build is also not special: it's simply the name of the target.

Run Tasks Affected by a PR

You can also run a command for all the projects affected by your PR like this:

npx nx affected --target=test

Learn more about the affected command here.

Control How Tasks Run

For more control over the order tasks are executed, edit the Task Pipeline Configuration.

To speed up your task execution, learn how to Cache Task Results and Distribute Task Execution