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-appcreates a new application namedmy-appin the/my-appfoldernx g lib my-libcreates a new library namedmy-libin the/my-libfoldernx g app my-app --directory=apps/nested/my-appcreates a new application namedmy-appin the/apps/nested/my-appfoldernx g lib my-lib --directory=libs/shared/ui/my-libcreates a new library namedmy-libin the/libs/shared/ui/my-libfolder
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}
7These 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-appcreates a new application namedmy-appin the/demos/my-appfoldernx g lib my-libcreates a new library namedmy-libin the/packages/my-libfoldernx g app my-app --directory=nestedcreates a new application namednested-my-appin the/demos/nested/my-appfoldernx g lib my-lib --directory=shared/uicreates a new library namedshared-ui-my-libin the/packages/shared/ui/my-libfolder
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