Workspace Layout and projectNameAndRootFormat
As of Nx 16.8.0, generating a project will prompt you to choose how Nx will calculate where the project should be located. You can choose between as-provided
and derived
. as-provided
will be the default in Nx 18.
as-provided
This setting makes app or lib generators behave in the following way:
nx g app my-app
creates a new application namedmy-app
in the/my-app
foldernx g lib my-lib
creates a new library namedmy-lib
in the/my-lib
foldernx g app my-app --directory=apps/nested/my-app
creates a new application namedmy-app
in the/apps/nested/my-app
foldernx g lib my-lib --directory=libs/shared/ui/my-lib
creates a new library namedmy-lib
in the/libs/shared/ui/my-lib
folder
derived
Choosing derived
makes Nx behave the way it did before version 16.8.0. Nx will check the workspaceLayout
property in nx.json
to determine how it should calculate the path to the new project.
1{
2 "workspaceLayout": {
3 "appsDir": "demos",
4 "libsDir": "packages"
5 }
6}
7
These settings would store apps in /demos/
and libraries in /packages/
. The paths specified are relative to the workspace root.
This makes app or lib generators behave in the following way:
nx g app my-app
creates a new application namedmy-app
in the/demos/my-app
foldernx g lib my-lib
creates a new library namedmy-lib
in the/packages/my-lib
foldernx g app my-app --directory=nested
creates a new application namednested-my-app
in the/demos/nested/my-app
foldernx g lib my-lib --directory=shared/ui
creates a new library namedshared-ui-my-lib
in the/packages/shared/ui/my-lib
folder
If you accidentally generate a project in the wrong folder, use the move generator to move it to the correct location.
Skipping the Prompt
You can skip the prompt in two ways:
- Specify a flag directly in the terminal
nx g app my-app --directory=apps/my-app --projectNameAndRootFormat=as-provided
- Set a default value for that property for every generator that you use.
1{
2 "generators": {
3 "@nx/node": {
4 "application": {
5 "projectNameAndRootFormat": "as-provided"
6 },
7 "library": {
8 "projectNameAndRootFormat": "as-provided"
9 }
10 },
11 "@nx/react": {
12 "application": {
13 "projectNameAndRootFormat": "as-provided"
14 },
15 "library": {
16 "projectNameAndRootFormat": "as-provided"
17 }
18 }
19 // etc
20 }
21}
22