nx.json
The nx.json
file configures the Nx CLI and project defaults. The full machine readable schema is available on GitHub.
The following is an expanded example showing all options. Your nx.json
will likely be much shorter. For a more intuitive understanding of the roles of each option, you can highlight the options in the excerpt below that relate to different categories.
1{
2 "extends": "nx/presets/npm.json",
3 "affected": {
4 "defaultBase": "main"
5 },
6 "generators": {
7 "@nx/js:library": {
8 "buildable": true
9 }
10 },
11 "namedInputs": {
12 "default": ["{projectRoot}/**/*"],
13 "production": ["!{projectRoot}/**/*.spec.tsx"]
14 },
15 "targetDefaults": {
16 "build": {
17 "inputs": ["production", "^production"],
18 "dependsOn": ["^build"],
19 "executor": "@nrwl/js:tsc",
20 "options": {
21 "main": "{projectRoot}/src/index.ts"
22 },
23 "cache": true
24 }
25 },
26 "parallel": 4,
27 "cacheDirectory": "tmp/my-nx-cache",
28 "release": {
29 "version": {
30 "generatorOptions": {
31 "currentVersionResolver": "git-tag",
32 "specifierSource": "conventional-commits"
33 }
34 },
35 "changelog": {
36 "git": {
37 "commit": true,
38 "tag": true
39 },
40 "workspaceChangelog": {
41 "createRelease": "github"
42 },
43 "projectChangelogs": true
44 }
45 }
46}
47
Extends
Some presets use the extends
property to hide some default options in a separate json file. The json file specified in the extends
property is located in your node_modules
folder. The Nx preset files are specified in the nx
package.
NPM Scope
The npmScope
property of the nx.json
file is deprecated as of version 16.2.0. npmScope
was used as a prefix for the names of newly created projects. The new recommended way to define the organization prefix is to set the name
property in the root package.json
file to @my-org/root
. Then @my-org/
will be used as a prefix for all newly created projects.
In Nx 16, if the npmScope
property is present, it will be used as a prefix. If the npmScope
property is not present, the name
property of the root package.json
file will be used to infer the prefix.
In Nx 17, the npmScope
property is ignored.
Affected
Tells Nx which branch and HEAD to use when calculating affected projects.
defaultBase
defines the default base branch, defaulted tomain
.
inputs & namedInputs
Named inputs defined in nx.json
are merged with the named inputs defined in each project's project.json. In other words, every project has a set of named inputs, and it's defined as: {...namedInputsFromNxJson, ...namedInputsFromProjectsProjectJson}
.
Defining inputs
for a given target would replace the set of inputs for that target name defined in nx.json
. Using pseudocode inputs = projectJson.targets.build.inputs || nxJson.targetDefaults.build.inputs
.
You can also define and redefine named inputs. This enables one key use case, where your nx.json
can define things like this (which applies to every project):
1"test": {
2 "inputs": [
3 "default",
4 "^production"
5 ]
6}
7
And projects can define their production
inputs, without having to redefine the inputs for the test
target.
1{
2 "namedInputs": {
3 "production": ["default", "!{projectRoot}/**/*.test.js"]
4 }
5}
6
In this case Nx will use the right production
input for each project.
inputs and namedInputs are also described in the project configuration reference
This guide walks through a few examples of how to customize inputs and namedInputs
Target Defaults
Target defaults provide ways to set common options for a particular target in your workspace. When building your project's configuration, we merge it with up to 1 default from this map. For a given target, we look at its name and its executor. We then check target defaults for any of the following combinations:
`${executor}`
`${targetName}`
Whichever of these we find first, we use as the base for that target's configuration. Some common scenarios for this follow.
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 project.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 targetDefaults
in nx.json
is helpful.
1{
2 "targetDefaults": {
3 "build": {
4 "dependsOn": ["^build"]
5 }
6 }
7}
8
The configuration above is identical to adding {"dependsOn": ["^build"]}
to every build target of every project.
For full documentation of the dependsOn
property, see the project configuration reference.
Another target default you can configure is outputs
:
1{
2 "targetDefaults": {
3 "build": {
4 "outputs": ["{projectRoot}/custom-dist"]
5 }
6 }
7}
8
When defining any options or configurations inside of a target default, you may use the {workspaceRoot}
and {projectRoot}
tokens. This is useful for defining things like the outputPath or tsconfig for many build targets.
1{
2 "targetDefaults": {
3 "@nx/js:tsc": {
4 "options": {
5 "main": "{projectRoot}/src/index.ts"
6 },
7 "configurations": {
8 "prod": {
9 "tsconfig": "{projectRoot}/tsconfig.prod.json"
10 }
11 },
12 "inputs": ["prod"],
13 "outputs": ["{workspaceRoot}/{projectRoot}"]
14 },
15 "build": {
16 "inputs": ["prod"],
17 "outputs": ["{workspaceRoot}/{projectRoot}"],
18 "cache": true
19 }
20 }
21}
22
Note that the inputs and outputs are respecified on the @nx/js:tsc default configuration. This is required, as when reading target defaults Nx will only ever look at one key. If there is a default configuration based on the executor used, it will be read first. If not, Nx will fall back to looking at the configuration based on target name. For instance, running nx build project
will read the options from targetDefaults[@nx/js:tsc]
if the target configuration for build uses the @nx/js:tsc executor. It would not read any of the configuration from the build
target default configuration unless the executor does not match.
Cache
In Nx 17 and higher, caching is configured by specifying "cache": true
in a target's configuration. This will tell Nx that it's ok to cache the results of a given target. For instance, if you have a target that runs tests, you can specify "cache": true
in the target default configuration for test
and Nx will cache the results of running tests.
1{
2 "targetDefaults": {
3 "test": {
4 "cache": true
5 }
6 }
7}
8
If you are using distributed task execution and disable caching for a given target, you will not be able to use distributed task execution for that target. This is because distributed task execution requires caching to be enabled. This means that the target you have disabled caching for, and any targets which depend on that target will fail the pipeline if you try to run them with DTE enabled.
Plugins
Nx plugins can provide generators, executors, as well as modifying the project graph. Any plugin that modifies the project graph must be listed in the plugins
array in nx.json
. Plugins which modify the project graph generally either add nodes or dependencies to the graph.
This can be read about in more detail in the plugins guide.
Inside nx.json
, these plugins are either listed by their module path, or an object that references the plugin's module path and options that should be passed to it.
1{
2 "plugins": [
3 "@my-org/graph-plugin",
4 {
5 "plugin": "@my-org/other-plugin",
6 "options": {
7 "someOption": true
8 }
9 }
10 ]
11}
12
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 "@nx/js:library": {
4 "buildable": true
5 }
6 }
7}
8
Tasks Runner Options
A task is an invocation of a target.
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 like this nx run-many -t build --runner=another
.
To register a tasks runner, add it to nx.json
like this:
1{
2 "tasksRunnerOptions": {
3 "another": {
4 "runner": "nx/tasks-runners/default",
5 "options": {}
6 }
7 }
8}
9
Tasks runners can accept different options. The following are the options supported by "nx/tasks-runners/default"
and "nx-cloud"
.
As of Nx 17, if you only use one tasks runner, you can specify these properties at the root of nx.json
instead of inside the tasksRunnerOptions
property.
Property | Description |
---|---|
cacheableOperations | In Nx < 17, defined the list of targets/operations that were cached by Nx. In Nx 17, use the cache property in targetDefaults or individual target definitions |
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 (defaults to node_modules/.cache/nx ) |
encryptionKey | (when using "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 |
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 ) |
You can configure parallel
in nx.json
, but you can also pass them in the terminal nx run-many -t test --parallel=5
.
Release
The release
property in nx.json
configures the nx release
command. It is an optional property, as nx release
is capable of working with zero config, but when present it is used to configure the versioning, changelog, and publishing phases of the release process.
For more information on how nx release
works, see manage releases.
The full list of configuration options available for "release"
can be found here: https://github.com/nrwl/nx/blob/master/packages/nx/src/config/nx-json.ts under NxReleaseConfiguration
.
Projects
If you want to limit the projects that nx release
targets, you can use the projects
property in nx.json
to do so. This property is either a string, or an array of strings. The strings can be project names, glob patterns, directories, tag references or anything else that is supported by the --projects
filter you may know from other commands such as nx run
.
1{
2 "release": {
3 // Here we are configuring nx release to target all projects
4 // except the one called "ignore-me"
5 "projects": ["*", "!ignore-me"]
6 }
7}
8
Projects Relationship
The projectsRelationship
property tells Nx whether to release projects independently or together. By default Nx will release all your projects together in lock step, which is an equivalent of "projectRelationships": "fixed"
. If you want to release projects independently, you can set "projectsRelationship": "independent"
.
1{
2 "release": {
3 // Here we are configuring nx release to release projects
4 // independently, as opposed to the default of "fixed"
5 "projectsRelationship": "independent"
6 }
7}
8
Release Tag Pattern
Optionally override the git/release tag pattern to use. This field is the source of truth for changelog generation and release tagging, as well as for conventional commits parsing.
It supports interpolating the version as {version}
and (if releasing independently or forcing project level version control system releases) the project name as {projectName}
within the string.
The default "releaseTagPattern"
for fixed/unified releases is: v{version}
The default "releaseTagPattern"
for independent releases at the project level is: {projectName}@{version}
1{
2 "release": {
3 // Here we are configuring nx release to use a custom release
4 // tag pattern (we have dropped the v prefix from the default)
5 "releaseTagPattern": "{version}"
6 }
7}
8
Version
The version
property configures the versioning phase of the release process. It is used to determine the next version of your projects, and update any projects that depend on them to use the new version.
Behind the scenes, the version
logic is powered by an Nx generator. Out of the box Nx wires up the most widely applicable generator implementation for you, which is @nx/js:release-version
provided by the @nx/js
plugin.
It is therefore a common requirement to be able to tweak the options given to that generator. This can be done by configuring the release.version.generatorOptions
property in nx.json
:
1{
2 "release": {
3 "version": {
4 "generatorOptions": {
5 // Here we are configuring the generator to use git tags as the
6 // source of truth for a project's current version
7 "currentVersionResolver": "git-tag",
8 // Here we are configuring the generator to use conventional
9 // commits as the source of truth for how to determine the
10 // relevant version bump for the next version
11 "specifierSource": "conventional-commits"
12 }
13 }
14 }
15}
16
For a full reference of the available options for the @nx/js:release-version
generator, see the release version generator reference.
Changelog
The changelog
property configures the changelog phase of the release process. It is used to generate a changelog for your projects, and commit it to your repository.
There are two types of possible changelog that can be generated:
Workspace Changelog: A changelog that contains all changes across all projects in your workspace. This is not applicable when releasing projects independently.
Project Changelogs: A changelog that contains all changes for a given project.
The changelog
property is used to configure both of these changelogs.
Workspace Changelog
The changelog.workspaceChangelog
property configures the workspace changelog. It is used to determine if and how the workspace changelog is generated.
1{
2 "release": {
3 "changelog": {
4 // This disables the workspace changelog
5 "workspaceChangelog": false
6 }
7 }
8}
9
1{
2 "release": {
3 "changelog": {
4 "workspaceChangelog": {
5 // This will create a GitHub release containing the workspace
6 // changelog contents
7 "createRelease": "github",
8 // This will disable creating a workspace CHANGELOG.md file
9 "file": false
10 }
11 }
12 }
13}
14
Project Changelogs
The changelog.projectChangelogs
property configures the project changelogs. It is used to determine if and how the project changelogs are generated.
1{
2 "release": {
3 "changelog": {
4 // This enables project changelogs with the default options
5 "projectChangelogs": true
6 }
7 }
8}
9
1{
2 "release": {
3 "changelog": {
4 "projectChangelogs": {
5 // This will create one GitHub release per project containing
6 // the project changelog contents
7 "createRelease": "github",
8 // This will disable creating any project level CHANGELOG.md
9 // files
10 "file": false
11 }
12 }
13 }
14}
15
Git
The git
property configures the automated git operations that take place as part of the release process.
1{
2 "release": {
3 "git": {
4 // This will enable committing any changes (e.g. package.json
5 // updates, CHANGELOG.md files) to git
6 "commit": true,
7 // This will enable create a git for the overall release, or
8 // one tag per project for independent project releases
9 "tag": false
10 }
11 }
12}
13