0
# Project Generators
1
2
Core generators for creating, moving, and managing projects within Nx workspaces. These generators handle project lifecycle management, from creation to removal, and provide essential workspace organization capabilities.
3
4
## Capabilities
5
6
### Move Generator
7
8
Moves applications or libraries to different folders within the workspace, updating all references and configurations.
9
10
```typescript { .api }
11
/**
12
* Move an application or library to another folder
13
* @param tree - The file system tree
14
* @param schema - Configuration for the move operation
15
* @returns Promise resolving to generator callback
16
*/
17
function moveGenerator(tree: Tree, schema: MoveSchema): Promise<GeneratorCallback>;
18
19
interface MoveSchema {
20
/** Name of the project to move */
21
projectName: string;
22
/** New destination path for the project */
23
destination: string;
24
/** Import path for the project */
25
importPath?: string;
26
/** Update import paths in other projects */
27
updateImportPath: boolean;
28
/** Skip formatting after move */
29
skipFormat?: boolean;
30
/** New project name after move */
31
newProjectName?: string;
32
}
33
```
34
35
**Usage Example:**
36
37
```typescript
38
import { Tree } from "@nx/devkit";
39
import { moveGenerator } from "@nx/workspace";
40
41
// Move a library to a different folder
42
await moveGenerator(tree, {
43
projectName: "shared-utils",
44
destination: "libs/common/shared-utils",
45
updateImportPath: true
46
});
47
```
48
49
### Remove Generator
50
51
Removes applications or libraries from the workspace, cleaning up all references and configurations.
52
53
```typescript { .api }
54
/**
55
* Remove an application or library from the workspace
56
* @param tree - The file system tree
57
* @param schema - Configuration for the remove operation
58
* @returns Promise resolving when removal is complete
59
*/
60
function removeGenerator(tree: Tree, schema: RemoveSchema): Promise<void>;
61
62
interface RemoveSchema {
63
/** Name of the project to remove */
64
projectName: string;
65
/** Skip formatting after removal */
66
skipFormat: boolean;
67
/** Force removal even if project has dependents */
68
forceRemove: boolean;
69
/** Import path for the project */
70
importPath?: string;
71
}
72
```
73
74
**Usage Example:**
75
76
```typescript
77
import { Tree } from "@nx/devkit";
78
import { removeGenerator } from "@nx/workspace";
79
80
// Remove a project from the workspace
81
await removeGenerator(tree, {
82
projectName: "old-feature-lib",
83
skipFormat: false,
84
forceRemove: false
85
});
86
```
87
88
### Run Commands Generator
89
90
Generates targets to run shell commands in the terminal, useful for custom build scripts and automation.
91
92
```typescript { .api }
93
/**
94
* Generates a target to run any command in the terminal
95
* @param tree - The file system tree
96
* @param schema - Configuration for the run commands target
97
* @returns Promise resolving when target is created
98
*/
99
function runCommandsGenerator(tree: Tree, schema: RunCommandsSchema): Promise<void>;
100
101
interface RunCommandsSchema {
102
/** Name of the target */
103
name: string;
104
/** Command to execute */
105
command: string;
106
/** Name of the project to add the target to */
107
project: string;
108
/** Working directory for the command */
109
cwd?: string;
110
/** Output paths for caching */
111
outputs?: string;
112
/** Environment file to load */
113
envFile?: string;
114
}
115
```
116
117
**Usage Example:**
118
119
```typescript
120
import { Tree } from "@nx/devkit";
121
import { runCommandsGenerator } from "@nx/workspace";
122
123
// Add a custom build target
124
await runCommandsGenerator(tree, {
125
project: "my-app",
126
name: "docker-build",
127
command: "docker build -t my-app .",
128
cwd: "apps/my-app",
129
outputs: "dist/apps/my-app"
130
});
131
```
132
133
### Convert to Nx Project Generator
134
135
Converts existing projects to use Nx project configuration format, fixing configuration issues.
136
137
```typescript { .api }
138
/**
139
* Fixes projects configuration to use Nx format
140
* @param tree - The file system tree
141
* @param schema - Configuration for the conversion
142
* @returns Promise resolving when conversion is complete
143
*/
144
function convertToNxProjectGenerator(tree: Tree, schema: ConvertSchema): Promise<void>;
145
146
interface ConvertSchema {
147
/** Name of the project to convert */
148
project?: string;
149
/** Convert all projects */
150
all?: boolean;
151
/** Reformat configuration files */
152
reformat?: boolean;
153
/** Skip formatting after conversion */
154
skipFormat?: boolean;
155
}
156
```
157
158
**Usage Example:**
159
160
```typescript
161
import { Tree } from "@nx/devkit";
162
import { convertToNxProjectGenerator } from "@nx/workspace";
163
164
// Fix configuration for a specific project
165
await convertToNxProjectGenerator(tree, {
166
project: "legacy-app",
167
reformat: true
168
});
169
170
// Fix all projects in workspace
171
await convertToNxProjectGenerator(tree, {
172
all: true,
173
reformat: true
174
});
175
```
176
177
### NPM Package Generator
178
179
Creates minimal NPM packages within the workspace for publishing libraries.
180
181
```typescript { .api }
182
/**
183
* Create a minimal NPM package
184
* @param tree - The file system tree
185
* @param options - Configuration for the package
186
* @returns Promise resolving to generator callback
187
*/
188
function npmPackageGenerator(tree: Tree, options: ProjectOptions): Promise<GeneratorCallback>;
189
190
interface ProjectOptions {
191
/** Directory where the package should be created */
192
directory: string;
193
/** Name of the package (optional, derived from directory if not provided) */
194
name?: string;
195
}
196
```
197
198
**Usage Example:**
199
200
```typescript
201
import { Tree } from "@nx/devkit";
202
import { npmPackageGenerator } from "@nx/workspace";
203
204
// Create a new NPM package
205
const callback = await npmPackageGenerator(tree, {
206
directory: "libs/my-package",
207
name: "my-package"
208
});
209
210
// Execute any post-generation tasks
211
if (callback) {
212
callback();
213
}
214
```
215
216
## Generator Configuration
217
218
### Available CLI Generators
219
220
All generators are available through the Nx CLI and can be configured via `generators.json`:
221
222
- **preset**: Create application in empty workspace (hidden)
223
- **move**: Move projects between folders (aliases: `mv`)
224
- **remove**: Remove projects from workspace (aliases: `rm`)
225
- **convert-to-monorepo**: Convert project to monorepo structure
226
- **new**: Create new workspace (hidden)
227
- **run-commands**: Create terminal command targets (aliases: `run-command`, `target`)
228
- **fix-configuration**: Fix project configurations (aliases: `convert-to-nx-project`)
229
- **npm-package**: Create minimal NPM packages
230
- **ci-workflow**: Generate CI workflow configurations
231
- **infer-targets**: Convert projects to use inferred targets
232
233
### Schema Validation
234
235
Each generator includes JSON schema validation for its options, ensuring type safety and proper configuration validation at runtime.
236
237
## Types
238
239
```typescript { .api }
240
interface MoveSchema {
241
projectName: string;
242
destination: string;
243
importPath?: string;
244
updateImportPath: boolean;
245
skipFormat?: boolean;
246
newProjectName?: string;
247
}
248
249
interface RemoveSchema {
250
projectName: string;
251
skipFormat: boolean;
252
forceRemove: boolean;
253
importPath?: string;
254
}
255
256
interface RunCommandsSchema {
257
name: string;
258
command: string;
259
project: string;
260
cwd?: string;
261
outputs?: string;
262
envFile?: string;
263
}
264
265
interface ConvertSchema {
266
project?: string;
267
all?: boolean;
268
reformat?: boolean;
269
skipFormat?: boolean;
270
}
271
272
interface ProjectOptions {
273
directory: string;
274
name?: string;
275
}
276
```