Use Task Executors

Executors perform actions on your code. This can include building, linting, testing, serving and many other actions.

There are two main differences between an executor and a shell script or an npm script:

  1. Executors encourage a consistent methodology for performing similar actions on unrelated projects. i.e. A developer switching between teams can be confident that nx build project2 will build project2 with the default settings, just like nx build project1 built project1.
  2. Nx can leverage this consistency to run the same target across multiple projects. i.e. nx affected --target=test will run the test executor associated with the test target on every project that is affected by the current code change.
  3. Executors provide metadata to define the available options. This metadata allows the Nx CLI to show prompts in the terminal and Nx Console to generate a GUI for the executor.

Executor definitions

Executors are associated with specific targets in a project's project.json file.

1{
2  "root": "apps/cart",
3  "sourceRoot": "apps/cart/src",
4  "projectType": "application",
5  "generators": {},
6  "targets": {
7    "build": {
8      "executor": "@nrwl/web:webpack",
9      "options": {
10        "outputPath": "dist/apps/cart",
11        ...
12      },
13      "configurations": {
14        "production": {
15          "sourceMap": false,
16          ...
17        }
18      }
19    },
20    "test": {
21      "executor": "@nrwl/jest:jest",
22      "options": {
23        ...
24      }
25    }
26  }
27}
28

Each project has targets configured to run an executor with a specific set of options. In this snippet, cart has two targets defined - build and test.

Each executor definition has an executor property and, optionally, an options and a configurations property.

  • executor is a string of the form [package name]:[executor name]. For the build executor, the package name is @nrwl/web and the executor name is webpack.
  • options is an object that contains any configuration defaults for the executor. These options vary from executor to executor.
  • configurations allows you to create presets of options for different scenarios. All the configurations start with the properties defined in options as a baseline and then overwrite those options. In the example, there is a production configuration that overrides the default options to set sourceMap to false.

Running executors

The nx run cli command (or the shorthand versions) can be used to run executors.

nx run [project]:[command]
nx run cart:build

As long as your command name doesn't conflict with an existing nx cli command, you can use this short hand:

nx [command] [project]
nx build cart

You can also use a specific configuration preset like this:

nx [command] [project] --configuration=[configuration]
nx build cart --configuration=production

Or you can overwrite individual executor options like this:

nx [command] [project] --[optionNameInCamelCase]=[value]
nx build cart --outputPath=some/other/path

Concepts

Recipes

Reference