Nx comes with dedicated documentation for each framework:

Configuration

There are two main types of configuration in every Nx workspace: project configuration and the global Nx CLI configuration.

Many Nx plugins modify these files when generating new code, but you can also modify them manually.

Project Configuration

Project configuration is defined in the package.json and project.json files located in each project's folder. Nx merges the two files to get each project's configuration.

If you don't use any Nx plugins, your project configuration will be defined in its package.json. If you use Nx plugins, the relevant configuration will be defined in project.json.

package json

Every npm script defined in package.json is a target you can invoke via Nx. For instance, if your project has the following package.json:

1{
2  "name": "mylib",
3  "scripts": {
4    "test: "jest",
5    "build": "tsc -p tsconfig.lib.json" // the actual command here is arbitrary
6  }
7}

you can invoke nx clean mylib or nx test mylib without any extra configuration.

You can add Nx-specific configuration as follows:

1{
2  "name": "mylib",
3  "scripts": {
4    "test: "jest",
5    "build": "tsc -p tsconfig.lib.json" // the actual command here is arbitrary
6  },
7  "nx": {
8    "targets": {
9      "build": {
10        "outputs": ["dist/libs/mylib"],
11        "dependsOn": [
12          {
13            "target": "build",
14            "projects": "dependencies"
15          }
16        ]
17      }
18    }
19  }
20}

This tells Nx that the build target of mylib depends on the same target of all mylib's dependencies, so they always have to be built first. It also tells Nx that the build is going to create files in dist/libs/mylib.

The configuration above is actually not needed. Nx comes with reasonable defaults (imported in nx.json) which implement the configuration above.

Another common thing is to define dependencies between targets of the same project:

1{
2  "name": "mylib",
3  "scripts": {
4    "test: "jest",
5    "build": "tsc -p tsconfig.lib.json" // the actual command here is arbitrary
6  },
7  "nx": {
8    "targets": {
9      "test": {
10        "dependsOn": {
11          "target": "build",
12          "projects": "self"
13        }
14      }
15    }
16  }
17}

Adding Tags and Implicit Dependencies

1{
2  "name": "mylib",
3  "nx": {
4    "tags": ["scope:myteam"],
5    "implicitDependencies": ["anotherlib"]
6  }
7}
  • tags configures tags used for linting
  • implicitDependencies configure implicit dependencies between projects in the workspace (see below)

Ignoring a project

Nx will add every project with a package.json file in it to its project graph. If you want to ignore a particular project, add the following to its package.json:

1{
2  "name": "mylib",
3  "nx": {
4    "ignore": true
5  }
6}

project json

The project.json file contains configuration specific to its project. This file is often created when you use Nx Plugins. Everything you can configure in package.json you can also configure in project.json. In addition, you can configure custom executors, which are used instead of npm scripts. Custom executors are typed, toolable and provide a lot more flexibility for running long-live processes. They are also more composable.

If you satisfied with npm scripts though, you will never see a project.json file in your workspace. But we encourage you to explore Nx Plugins and the power they bring.

Let's look at the following project.json:

1{
2  "root": "libs/mylib/",
3  "sourceRoot": "libs/mylib/src",
4  "projectType": "library",
5  "targets": {},
6  "tags": [],
7  "implicitDependencies": []
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.
  • tags configures tags used for linting
  • implicitDependencies configure implicit dependencies between projects in the workspace (see below)

Targets

Let's look at a sample test target:

1{
2  "test": {
3    "executor": "@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 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 will be passed to the executor. 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 executor:

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 executor creates files in. The property is optional. If not provided, Nx assumes it is dist/app/myapp or dist/libs/mylib.

1{
2  "build": {
3    "executor": "@nrwl/js:tsc",
4    "outputs": ["dist/apps/myapp"],
5    "options": {
6      "tsConfig": "apps/myapp/tsconfig.app.json",
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    "executor": "@nrwl/js:tsc",
4    "outputs": ["dist/apps/myapp"],
5    "options": {
6      "tsConfig": "apps/myapp/tsconfig.app.json",
7      "main": "apps/myapp/src/main.ts"
8    },
9    "configurations": {
10      "production": {
11        "tsConfig": "apps/myapp/tsconfig-prod.app.json"
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 executor options get constructed:

1require(`@nrwl/jest`).executors['jest']({ ...options, ...selectedConfiguration, ...commandLineArgs }
2}) // 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/js:tsc",
4    "outputs": ["dist/apps/myapp"],
5    "options": {
6      "tsConfig": "apps/myapp/tsconfig.app.json",
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 flag has 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/js:tsc",
4    "outputs": ["dist/apps/myapp"],
5    "options": {
6      "tsConfig": "apps/myapp/tsconfig.app.json",
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).

workspace json

The workspace.json is optional. It's used if you want to list the projects in your workspace explicitly instead of Nx scanning the file tree for all project.json and package.json files.

1{
2  "version": 2,
3  "projects": {
4    "myapp": "apps/myapp"
5  }
6}
  • "version": 2 tells Nx that we are using Nx's format for the workspace.json file.
  • projects is a map of project name to either the project location, or its configuration. ( see project.json)

You could inline project.json files into workspace.json. This used to be the default, but it's no longer recommended. If you have an existing workspace where the configuration is inlined, run nx g convert-to-nx-project --all.

If you have an old workspace where the configuration version is set to 1, change the version number to 2 and run nx format.

CLI Configuration

nx json

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

The following is an expanded version showing all options. Your nx.json will likely be much shorter.

1{
2  "npmScope": "happyorg",
3  "affected": {
4    "defaultBase": "main"
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  "targetDependencies": {
24    "build": [
25      {
26        "target": "build",
27        "projects": "dependencies"
28      }
29    ]
30  },
31  "workspaceLayout": {
32    "appsDir": "demos",
33    "libsDir": "packages"
34  },
35  "cli": {
36    "defaultCollection": "@nrwl/js"
37  }
38}

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 main.

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 the max number of targets ran in parallel (in older versions of Nx you had to pass --parallel --maxParallel=3 instead of --parallel=3)
  • 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 in nx.json, but you can also pass them in the terminal nx run-many --target=test --parallel=5.

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    "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 in project configuration. 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  //... other project config
3  "tags": [],
4  "implicitDependencies": ["myapp"]
5}

Finally, you can remove dependencies between projects. The following say even though the project imports 'mylib', it's not a dependency Nx should be concerned with.

1{
2  //... other project config
3  "tags": [],
4  "implicitDependencies": ["!mylib"]
5}

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.

CLI Options

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

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

Generators

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

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

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.

Keeping the configuration in sync

When creating projects, the Nx generators make sure these configuration files are updated accordingly for the new projects. While development continues and the workspace grows, you might need to refactor projects by renaming them, moving them to a different folder, removing them, etc. When this is done manually, you need to ensure your configuration files are kept in sync and that's a cumbersome task. Fortunately, Nx provides some generators and executors to help you with these tasks.

Moving projects

Projects can be moved or renamed using the @nrwl/workspace:move generator.

For instance, if a library under the booking folder is now being shared by multiple apps, you can move it to the shared folder like this:

nx g @nrwl/workspace:move --project booking-some-library shared/some-library

Removing projects

Projects can be removed using the @nrwl/workspace:remove generator.

nx g @nrwl/workspace:remove booking-some-library

Validating the configuration

If at any point in time you want to check if your configuration is in sync, you can use the workspace-lint executor:

nx workspace-lint

This will identify any projects with no files in the configured project root folder, as well as any file that's not part of any project configured in the workspace.

Recent Changes

v13-3-0

  • workspace.json is now optional
    • projects can be inferred completely from package.json if workspace.json not present
  • Targets are now merged from package.json instead of only being used if the project has no targets defined.
  • Targets inferred from package.json can now have an extended configuration. See above

v13-0-0

Some settings were moved between workspace.json/project.json and nx.json.

  • tags / implicit dependencies are no longer in nx.json were moved from nx.json to project configuration.
  • cli and defaultProject moved to nx.json from workspace.json
  • Non-project specific generator defaults in workspace.json via the generators/schematics property moved to nx.json

v12-4-0

Standalone configuration and project.json introduced. See above

  • tags / implicit dependencies are no longer in nx.json for projects using project.json.