Configuration
There are three top-level configuration files every Nx workspace has: angular.json
, nx.json
, and tsconfig.base.json
. Many Nx plugins will modify these files when generating new code, but you can also modify them manually.
angular.json
The angular.json
configuration file contains information about the targets and generators. Let's look at the following example:
1{
2 "projects": {
3 "myapp": {
4 "root": "apps/myapp/",
5 "sourceRoot": "apps/myapp/src",
6 "projectType": "application",
7 "architect": {
8 "build": {
9 "builder": "@nrwl/web:build",
10 "outputs": ["dist/apps/myapp"],
11 "options": {
12 "index": "apps/myapp/src/app.html",
13 "main": "apps/myapp/src/main.ts"
14 },
15 "configurations": {
16 "production": {
17 "optimization": true
18 }
19 }
20 },
21 "serve": {
22 "builder": "@nrwl/web:dev-server",
23 "options": {
24 "buildTarget": "myapp:build",
25 "proxyConfig": "apps/myapp/proxy.conf.json"
26 }
27 },
28 "test": {
29 "builder": "@nrwl/jest:jest",
30 "options": {
31 "jestConfig": "apps/myapp/jest.config.js",
32 "tsConfig": "apps/myapp/tsconfig.spec.json"
33 }
34 }
35 }
36 },
37 "mylib": {
38 "root": "libs/mylib/",
39 "sourceRoot": "libs/mylib/src",
40 "projectType": "library",
41 "architect": {
42 "test": {
43 "builder": "@nrwl/jest:jest",
44 "options": {
45 "jestConfig": "libs/mylib/jest.config.js",
46 "tsConfig": "libs/mylib/tsconfig.spec.json"
47 }
48 }
49 }
50 }
51 },
52 "cli": {
53 "defaultCollection": "@nrwl/web"
54 }
55}
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 "architect": {}
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'.architect
configures all the targets which define what tasks you can run against the library.
Nx uses the architect library built by the Angular team at Google. The naming reflects that. Important to note: it's a general purpose library that does not have any dependency on Angular.
Targets
Let's look at the simple architect target:
1{
2 "test": {
3 "builder": "@nrwl/jest:jest",
4 "options": {
5 "jestConfig": "libs/mylib/jest.config.js",
6 "tsConfig": "libs/mylib/tsconfig.spec.json"
7 }
8 }
9}
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 will be able to run as follows: nx run mylib:mytest
.
Builder
The builder
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 builder named jest
and invoke it with the options.
Options
The options
provides a map of values that will be passed to the builder. The provided command line args will be merged into this map. I.e., nx test mylib --jestConfig=libs/mylib/another-jest.config.js
will pass the following to the builder:
1{
2 "jestConfig": "libs/mylib/another-jest.config.js",
3 "tsConfig": "libs/mylib/tsconfig.spec.json"
4}
Outputs
The outputs
property lists the folders the builder will create files in. The property is optional. If not provided, Nx will assume it is dist/libs/mylib
.
1{
2 "build": {
3 "builder": "@nrwl/web: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}
Configurations
The configurations
property provides extra sets of values that will be merged into the options map.
1{
2 "build": {
3 "builder": "@nrwl/web:build",
4 "outputs": ["dist/apps/myapp"],
5 "options": {
6 "index": "apps/myapp/src/app.html",
7 "main": "apps/myapp/src/main.ts"
8 },
9 "configurations": {
10 "production": {
11 "optimization": true
12 }
13 }
14 }
15}
You can select a configuration like this: nx build myapp --configuration=production
or nx run myapp:build:configuration=production
.
The following show how the builder options get constructed:
require(`@nrwl/jest`).builders['jest']({...options, ...selectedConfiguration, ...commandLineArgs}}) // Pseudocode
The selected configuration adds/overrides the default options, and the provided command line args add/override the configuration options.
Generators
Generators that are created using @angular-devkit
are called schematics. You can configure default generator options in angular.json
as well. For instance, the following will tell Nx to always pass --style=scss
when creating new libraries.
1{
2 "schematics": {
3 "@nrwl/angular:library": {
4 "style": "scss"
5 }
6 }
7}
CLI Options
The following command will generate a new library: nx g @nrwl/angular:lib mylib
. If you set the defaultCollection
property, you can generate the lib without mentioning the collection name: nx g lib mylib
.
1{
2 "cli": {
3 "defaultCollection": "@nrwl/angular"
4 }
5}
workspace.json
Your angular.json
file can be renamed to workspace.json
and Nx will process it in the same way. The workspace.json
has one additional top level property version
. Setting version
to 1 means the workspace.json
file syntax is identical to angular.json
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
.
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 "angular.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 tomaster
.
Tasks Runner Options
Tasks runners are invoked when you run nx test
, nx build
, nx run-many
, nx affected
, etc.. The tasks runner named "default" will be, unsurprisingly, used by default. But you can 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 will be cached by Nx.parallel
defines whether to run targets in parallelmaxParallel
defines the max number of processes used.captureStderr
defines whether the cache will capture stderr or just stdoutskipNxCache
defines whether the Nx Cache should be skipped. Defaults tofalse
cacheDirectory
defines where the local cache is stored, which isnode_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 keyNX_CLOUD_ENCRYPTION_KEY
that contains an encryption key as its value. The Nx Cloud task runner will normalize the key length, so any length of key is acceptable.runtimeCacheInputs
defines the list of commands that will be run by the runner to include into the computation hash value.
runtimeCacheInputs
can be 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
.
Workspace Layout
You can add a workspaceLayout
property to modify where libraries and apps are located.
1{
2 "workspaceLayout": {
3 "appsDir": "demos",
4 "libsDir": "packages"
5 }
6}
These settings would store apps in /demos/
and libraries in /packages/
. The paths specified are relative to the workspace root.
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 "angular.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
angular.json
will affect every project. - Changing the
dependencies
property inpackage.json
will affect every project. - Changing the
devDependencies
property inpackage.json
will only affectmylib
. - Changing any of the custom check
scripts
inpackage.json
will affect every project. - Changing
globalFile
will only affectmyapp
. - Changing any CSS file inside the
styles
directory will only affectmyapp
.
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}
.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:
- Changes to that file will not be taken into account in the
affected
calculations. - Even if the file is outside an app or library,
nx workspace-lint
will not warn about it.