The Workspace plugin contains executors and generators that are useful for any Nx workspace and serves as a foundation for other plugins.
npx @tessl/cli install tessl/npm-nx--workspace@21.4.00
# Nx Workspace
1
2
Nx Workspace is the core plugin for Nx monorepo build system, providing essential executors and generators that form the foundation for any Nx workspace. It serves as a base plugin that other Nx plugins build upon, offering utilities for project management, code generation, and build orchestration.
3
4
## Package Information
5
6
- **Package Name**: @nx/workspace
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install @nx/workspace`
10
11
## Core Imports
12
13
```typescript
14
import {
15
moveGenerator,
16
removeGenerator,
17
runCommandsGenerator,
18
convertToNxProjectGenerator,
19
npmPackageGenerator
20
} from "@nx/workspace";
21
```
22
23
For utilities:
24
25
```typescript
26
import {
27
readTsConfig,
28
ProjectType,
29
projectRootDir,
30
updateJsonFile,
31
copyFile,
32
renameSync,
33
createDirectory,
34
Linter,
35
LinterType,
36
names,
37
output,
38
readPackageJson
39
} from "@nx/workspace";
40
```
41
42
CommonJS:
43
44
```javascript
45
const {
46
moveGenerator,
47
removeGenerator,
48
runCommandsGenerator
49
} = require("@nx/workspace");
50
```
51
52
## Basic Usage
53
54
```typescript
55
import { Tree } from "@nx/devkit";
56
import { runCommandsGenerator, ProjectType, updateJsonFile } from "@nx/workspace";
57
58
// Create a run-commands target for a project
59
await runCommandsGenerator(tree, {
60
project: "my-app",
61
name: "custom-build",
62
command: "webpack --mode production",
63
cwd: "apps/my-app"
64
});
65
66
// Work with project types
67
const appsDir = projectRootDir(ProjectType.Application); // returns "apps"
68
const libsDir = projectRootDir(ProjectType.Library); // returns "libs"
69
70
// Update configuration files
71
updateJsonFile("./package.json", (json) => {
72
json.scripts.build = "nx build";
73
return json;
74
});
75
```
76
77
## Architecture
78
79
Nx Workspace is built around several key components:
80
81
- **Generators**: Code generation tools for creating, moving, and configuring projects
82
- **Executors**: Task runners for build, test, and other operations
83
- **Utilities**: File system operations, TypeScript configuration management, and CLI helpers
84
- **Project Management**: Tools for organizing monorepo structure and project types
85
- **Configuration Management**: Parsers and manipulators for workspace and project configurations
86
87
## Capabilities
88
89
### Project Generators
90
91
Core generators for creating, moving, and managing projects within Nx workspaces. Essential for workspace organization and project lifecycle management.
92
93
```typescript { .api }
94
function moveGenerator(tree: Tree, schema: Schema): Promise<GeneratorCallback>;
95
function removeGenerator(tree: Tree, schema: Schema): Promise<void>;
96
function runCommandsGenerator(tree: Tree, schema: Schema): Promise<void>;
97
function convertToNxProjectGenerator(tree: Tree, schema: Schema): Promise<void>;
98
function npmPackageGenerator(tree: Tree, options: ProjectOptions): Promise<GeneratorCallback>;
99
```
100
101
[Project Generators](./generators.md)
102
103
### File System Utilities
104
105
Safe file and directory operations with error handling and workspace-aware functionality.
106
107
```typescript { .api }
108
function updateJsonFile(path: string, callback: (json: any) => any): void;
109
function copyFile(file: string, target: string): void;
110
function renameSync(from: string, to: string, cb: (err: Error | null) => void): void;
111
function createDirectory(path: string): void;
112
```
113
114
[File System Utilities](./file-utilities.md)
115
116
### TypeScript Configuration
117
118
Tools for reading, parsing, and managing TypeScript configuration files across the workspace.
119
120
```typescript { .api }
121
function readTsConfig(tsConfigPath: string): ParsedCommandLine;
122
function getRootTsConfigPathInTree(tree: Tree): string | null;
123
function getRelativePathToRootTsConfig(tree: Tree, targetPath: string): string;
124
function getRootTsConfigFileName(): string | null;
125
```
126
127
[TypeScript Configuration](./typescript-config.md)
128
129
### Project Type Management
130
131
Enums and utilities for managing different types of projects in Nx workspaces.
132
133
```typescript { .api }
134
enum ProjectType {
135
Application = 'application',
136
Library = 'library'
137
}
138
139
function projectRootDir(projectType: ProjectType): string;
140
```
141
142
[Project Types](./project-types.md)
143
144
### CLI Configuration Utilities
145
146
Parsers and manipulators for workspace configuration and build targets.
147
148
```typescript { .api }
149
function getWorkspacePath(host: Tree): string;
150
function parseTarget(targetString: string): ParsedTarget;
151
function editTarget(targetString: string, callback: Function): string;
152
function serializeTarget(target: ParsedTarget): string;
153
```
154
155
[CLI Configuration](./cli-config.md)
156
157
### Linter Integration
158
159
Support for ESLint integration and linter configuration management.
160
161
```typescript { .api }
162
enum Linter {
163
EsLint = 'eslint',
164
None = 'none'
165
}
166
167
type LinterType = 'eslint' | 'none';
168
```
169
170
[Linter Integration](./linter.md)
171
172
### Output Utilities
173
174
Terminal output formatting utilities (re-exported from nx/src/utils/output).
175
176
```typescript { .api }
177
const output: {
178
log: (message: string) => void;
179
warn: (message: string) => void;
180
error: (message: string) => void;
181
success: (message: string) => void;
182
// ... other output utilities
183
};
184
```
185
186
### Package JSON Utilities
187
188
Utilities for reading package.json files (re-exported from nx/src/project-graph/file-utils).
189
190
```typescript { .api }
191
function readPackageJson(path?: string): any;
192
```
193
194
### Naming Utilities
195
196
String manipulation utilities for consistent naming conventions (re-exported from @nx/devkit).
197
198
```typescript { .api }
199
function names(name: string): Names;
200
201
interface Names {
202
name: string;
203
className: string;
204
propertyName: string;
205
constantName: string;
206
fileName: string;
207
}
208
```
209
210
## Types
211
212
```typescript { .api }
213
interface ParsedTarget {
214
project: string;
215
target: string;
216
config?: string;
217
}
218
219
interface ProjectOptions {
220
directory: string;
221
name?: string;
222
}
223
224
interface Schema {
225
[key: string]: any;
226
}
227
228
enum ProjectType {
229
Application = 'application',
230
Library = 'library'
231
}
232
233
enum Linter {
234
EsLint = 'eslint',
235
None = 'none'
236
}
237
238
type LinterType = 'eslint' | 'none';
239
240
interface Names {
241
name: string;
242
className: string;
243
propertyName: string;
244
constantName: string;
245
fileName: string;
246
}
247
```