Configuration

There are three top-level configuration files every Nx workspace has: workspace.json, nx.json, and tsconfig.base.json. Many Nx plugins modify these files when generating new code, but you can also modify them manually.

workspace.json

The workspace.json configuration file contains information about the targets and generators. Let's look at the following example:

1{
2  "version": 2,
3  "projects": {
4    "myapp": {
5      "root": "apps/myapp/",
6      "sourceRoot": "apps/myapp/src",
7      "projectType": "application",
8      "targets": {
9        "build": {
10          "executor": "@nrwl/node:build",
11          "outputs": ["dist/apps/myapp"],
12          "dependsOn": [
13            {
14              "target": "build",
15              "projects": "dependencies"
16            }
17          ],
18          "options": {
19            "outputPath": "dist/packages/myapp",
20            "main": "packages/myapp/src/main.ts",
21            "tsConfig": "packages/myapp/tsconfig.app.json",
22            "assets": ["packages/myapp/src/assets"]
23          },
24          "configurations": {
25            "production": {
26              "optimization": true
27            }
28          }
29        },
30        "serve": {
31          "executor": "@nrwl/node:execute",
32          "options": {
33            "buildTarget": "myapp:build"
34          }
35        },
36        "test": {
37          "executor": "@nrwl/jest:jest",
38          "options": {
39            "jestConfig": "apps/myapp/jest.config.js"
40          }
41        }
42      }
43    },
44    "mylib": {
45      "root": "libs/mylib/",
46      "sourceRoot": "libs/mylib/src",
47      "projectType": "library",
48      "targets": {
49        "test": {
50          "executor": "@nrwl/jest:jest",
51          "options": {
52            "jestConfig": "libs/mylib/jest.config.js",
53            "tsConfig": "libs/mylib/tsconfig.spec.json"
54          }
55        },
56        "build": {
57          "executor": "@nrwl/node:package",
58          "options": {
59            "outputPath": "dist/libs/mylib",
60            "tsConfig": "libs/mylib/tsconfig.lib.json",
61            "packageJson": "libs/mylib/package.json",
62            "main": "libs/mylib/src/index.ts",
63            "assets": ["libs/mylib/*.md"]
64          }
65        }
66      }
67    }
68  },
69  "cli": {
70    "defaultCollection": "@nrwl/node"
71  },
72  "generators": {
73    "@nrwl/node:library": {
74      "js": true
75    }
76  }
77}

Projects

The projects property configures all apps and libs.

For instance, the following configures mylib.

1{
2  "mylib": {
3    "root": "libs/mylib/",
4    "sourceRoot": "libs/mylib/src",
5    "projectType": "library",
6    "targets": {}
7  }
8}
  • root tells Nx the location of the library including its sources and configuration files.
  • sourceRoot tells Nx the location of the library's source files.
  • projectType is either 'application' or 'library'. The project type is used in dep graph viz and in a few aux commands.
  • targets configures all the targets which define what tasks you can run against the library.

Projects utilizing project.json files are not present in workspace.json.

Targets

Let's look at the simple target:

1{
2  "test": {
3    "executor": "@nrwl/jest:jest",
4    "options": {
5      "jestConfig": "libs/mylib/jest.config.js"
6    }
7  }
8}

Target Name

The name of the target test means that you can invoke it as follows: nx test mylib or nx run mylib:test. The name isn't significant in any other way. If you rename it to, for example, mytest, you run as follows: nx mytest mylib or nx run mylib:mytest.

Executor

The executor property tells Nx what function to invoke when you run the target. "@nrwl/jest:jest" tells Nx to find the @nrwl/jest package, find the executor named jest and invoke it with the options.

Options

The options provides a map of values that are passed to the executor. The provided command line args are merged into this map. For example, nx test mylib --jestConfig=libs/mylib/another-jest.config.js passes the following to the executor:

1{
2  "jestConfig": "libs/mylib/another-jest.config.js"
3}

Outputs

The outputs property lists the folders the executor creates files in. The property is optional. If not provided, Nx assumes it is dist/libs/mylib.

1{
2  "build": {
3    "executor": "@nrwl/node:package",
4    "options": {
5      "outputPath": "dist/libs/mylib",
6      "tsConfig": "libs/mylib/tsconfig.lib.json",
7      "packageJson": "libs/mylib/package.json",
8      "main": "libs/mylib/src/index.ts",
9      "assets": ["libs/mylib/*.md"]
10    }
11  }
12}

Configurations

The configurations property provides extra sets of values that are merged into the options map.

1{
2  "build": {
3    "executor": "@nrwl/node:package",
4    "options": {
5      "outputPath": "dist/libs/mylib",
6      "tsConfig": "libs/mylib/tsconfig.lib.json",
7      "packageJson": "libs/mylib/package.json",
8      "main": "libs/mylib/src/index.ts",
9      "assets": ["libs/mylib/*.md"]
10    }
11  },
12  "configurations": {
13    "production": {
14      "packageJson": "libs/mylib/package.prod.json"
15    }
16  }
17}

You can select a configuration like this: nx build mylib --configuration=production or nx run mylib:build:configuration=production.

The following show how the executor options get constructed:

require(`@nrwl/jest`).executors['jest']({...options, ...selectedConfiguration, ...commandLineArgs}}) // Pseudocode

The selected configuration adds/overrides the default options, and the provided command line args add/override the configuration options.

Target Dependencies

Targets can depend on other targets. A common scenario is having to build dependencies of a project first before building the project. You can specify this using the dependsOn.

1{
2  "build": {
3    "executor": "@nrwl/node:build",
4    "outputs": ["dist/apps/myapp"],
5    "options": {
6      "index": "apps/myapp/src/app.html",
7      "main": "apps/myapp/src/main.ts"
8    },
9    "dependsOn": [
10      {
11        "target": "build",
12        "projects": "dependencies"
13      }
14    ]
15  }
16}

In this case, running nx build myapp builds all the buildable libraries myapp depends on first. In other words, nx build myapp results in multiple tasks executing. The --parallel, and --max-parallel flags have the same effect as they would with run-many or affected.

It is also possible to define dependencies between the targets of the same project.

In the following example invoking nx build myapp builds all the libraries first, then nx build-base myapp is executed and only then nx build myapp is executed.

1{
2  "build-base": {
3    "executor": "@nrwl/node:build",
4    "outputs": ["dist/apps/myapp"],
5    "options": {
6      "index": "apps/myapp/src/app.html",
7      "main": "apps/myapp/src/main.ts"
8    }
9  },
10  "build": {
11    "executor": "@nrwl/workspace:run-commands",
12    "dependsOn": [
13      {
14        "target": "build",
15        "projects": "dependencies"
16      },
17      {
18        "target": "build-base",
19        "projects": "self"
20      }
21    ],
22    "options": {
23      "command": "./copy-readme-and-license.sh"
24    }
25  }
26}

Often the same dependsOn configuration has to be defined for every project in the repo. You can define it once in nx.json (see below).

Generators

Default generator options are configured in workspace.json as well. For instance, the following tells Nx to always pass --js when creating new libraries.

1{
2  "generators": {
3    "@nrwl/node:library": {
4      "buildable": true
5    }
6  }
7}

You can also do it on the project level:

1{
2  "mylib": {
3    "root": "libs/mylib/",
4    "sourceRoot": "libs/mylib/src",
5    "projectType": "library",
6    "generators": {
7      "@nrwl/node:lib": {
8        "moreOptions": true
9      }
10    },
11    "targets": {}
12  }
13}

CLI Options

The following command generates a new library: nx g @nrwl/node:lib mylib. After setting the defaultCollection property, the lib is generated without mentioning the collection name: nx g lib mylib.

1{
2  "cli": {
3    "defaultCollection": "@nrwl/node"
4  }
5}

Version

When the version of workspace.json is set to 2, targets, generators and executor properties are used instead of the version 1 properties architect, schematics and builder.

project.json

Project configurations can also be independent files, referenced by workspace.json. For instance, a workspace.json may contain projects configured as below.

1{
2  "projects": {
3    "mylib": "libs/mylib"
4  }
5}

This tells Nx that all configuration for that project is found in the libs/mylib/project.json file. This file contains a combination of the project's configuration from both workspace.json and nx.json.

1{
2  "root": "libs/mylib/",
3  "sourceRoot": "libs/mylib/src",
4  "projectType": "library",
5  "targets": {},
6  "tags": [],
7  "implicitDependencies": []
8}

nx.json

The nx.json file contains extra configuration options mostly related to the project graph.

1{
2  "npmScope": "happyorg",
3  "affected": {
4    "defaultBase": "master"
5  },
6  "tasksRunnerOptions": {
7    "default": {
8      "runner": "@nrwl/workspace/tasks-runners/default",
9      "options": {
10        "cacheableOperations": ["build", "lint", "test", "e2e"]
11      }
12    }
13  },
14  "implicitDependencies": {
15    "workspace.json": "*",
16    "package.json": {
17      "dependencies": "*",
18      "devDependencies": "*"
19    },
20    "tsconfig.base.json": "*",
21    "nx.json": "*"
22  },
23  "projects": {
24    "myapp": {
25      "tags": []
26    },
27    "mylib": {
28      "tags": []
29    },
30    "myapp-e2e": {
31      "tags": [],
32      "implicitDependencies": ["myapp"]
33    }
34  }
35}

NPM Scope

Tells Nx what prefix to use when generating library imports.

Affected

Tells Nx which branch and HEAD to use when calculating affected projects.

  • defaultBase defines the default base branch, defaulted to master.

Tasks Runner Options

Tasks runners are invoked when you run nx test, nx build, nx run-many, nx affected, and so on. The tasks runner named "default" is used by default. Specify a different one by passing --runner.

A task is an invocation of a target.

Tasks runners can accept different options. The following are the options supported by "@nrwl/workspace/tasks-runners/default" and "@nrwl/nx-cloud".

  • cacheableOperations defines the list of targets/operations that are cached by Nx.
  • parallel defines whether to run targets in parallel
  • maxParallel defines the max number of processes used.
  • captureStderr defines whether the cache captures stderr or just stdout
  • skipNxCache defines whether the Nx Cache should be skipped. Defaults to false
  • cacheDirectory defines where the local cache is stored, which is node_modules/.cache/nx by default.
  • encryptionKey (when using "@nrwl/nx-cloud" only) defines an encryption key to support end-to-end encryption of your cloud cache. You may also provide an environment variable with the key NX_CLOUD_ENCRYPTION_KEY that contains an encryption key as its value. The Nx Cloud task runner normalizes the key length, so any length of key is acceptable.
  • runtimeCacheInputs defines the list of commands that are run by the runner to include into the computation hash value.
  • selectivelyHashTsConfig only hash the path mapping of the active project in the tsconfig.base.json (e.g., adding/removing projects doesn't affect the hash of existing projects). Defaults to false

runtimeCacheInputs are set as follows:

1{
2  "tasksRunnerOptions": {
3    "default": {
4      "runner": "@nrwl/workspace/tasks-runners/default",
5      "options": {
6        "cacheableOperations": ["build", "lint", "test", "e2e"],
7        "runtimeCacheInputs": ["node -v"]
8      }
9    }
10  }
11}

You can configure parallel and maxParallel in nx.json, but you can also pass them in the terminal nx run-many --target=test --parallel.

Implicit Dependencies

Nx performs advanced source-code analysis to figure out the project graph of the workspace. So when you make a change, Nx can deduce what can be broken by this change. Some dependencies between projects and dependencies between shared files and projects cannot be inferred statically. You can configure those using implicitDependencies.

1{
2  "implicitDependencies": {
3    "workspace.json": "*",
4    "package.json": {
5      "dependencies": "*",
6      "devDependencies": {
7        "mypackage": ["mylib"]
8      },
9      "scripts": {
10        "check:*": "*"
11      }
12    },
13    "globalFile": ["myapp"],
14    "styles/**/*.css": ["myapp"]
15  }
16}

In the example above:

  • Changing workspace.json affects every project.
  • Changing the dependencies property in package.json affects every project.
  • Changing the devDependencies property in package.json only affects mylib.
  • Changing any of the custom check scripts in package.json affects every project.
  • Changing globalFile only affects myapp.
  • Changing any CSS file inside the styles directory only affects myapp.

You can also add dependencies between projects. For instance, the example below defines a dependency from myapp-e2e to myapp, such that every time myapp is affected, myapp-e2e is affected as well.

1{
2  "projects": {
3    "myapp": {
4      "tags": []
5    },
6    "myapp-e2e": {
7      "tags": [],
8      "implicitDependencies": ["myapp"]
9    }
10  }
11}

Projects utilizing project.json files are not present in nx.json.

Target Dependencies

Targets can depend on other targets. A common scenario is having to build dependencies of a project first before building the project. The dependsOn property in workspace.json can be used to define the list of dependencies of an individual target.

Often the same dependsOn configuration has to be defined for every project in the repo, and that's when defining targetDependencies in nx.json is helpful.

1{
2  "targetDependencies": {
3    "build": [
4      {
5        "target": "build",
6        "projects": "dependencies"
7      }
8    ]
9  }
10}

The configuration above is identical to adding {"dependsOn": [{"target": "build", "projects": "dependencies"]} to every build target in workspace.json.

The dependsOn property in workspace.json takes precedence over the targetDependencies in nx.json.

.nxignore

You may optionally add an .nxignore file to the root. This file is used to specify files in your workspace that should be completely ignored by Nx.

The syntax is the same as a .gitignore file.

When a file is specified in the .nxignore file:

  1. Changes to that file are not taken into account in the affected calculations.
  2. Even if the file is outside an app or library, nx workspace-lint won't warn about it.