0
# Project Types
1
2
Enums and utilities for managing different types of projects in Nx workspaces. These utilities provide standardized project categorization and directory organization.
3
4
## Capabilities
5
6
### Project Type Enumeration
7
8
Standardized project types for workspace organization.
9
10
```typescript { .api }
11
/**
12
* Enumeration of supported project types in Nx workspace
13
*/
14
enum ProjectType {
15
/** Applications - executable projects that can be served or built for deployment */
16
Application = 'application',
17
/** Libraries - reusable code packages that can be imported by other projects */
18
Library = 'library'
19
}
20
```
21
22
**Usage Example:**
23
24
```typescript
25
import { ProjectType } from "@nx/workspace";
26
27
// Check project type
28
function handleProject(type: string) {
29
switch (type) {
30
case ProjectType.Application:
31
console.log("This is an application project");
32
break;
33
case ProjectType.Library:
34
console.log("This is a library project");
35
break;
36
default:
37
console.log("Unknown project type");
38
}
39
}
40
41
// Use in generator schemas
42
interface ProjectSchema {
43
name: string;
44
type: ProjectType;
45
directory?: string;
46
}
47
```
48
49
### Directory Resolution
50
51
Get standard directory paths for different project types.
52
53
```typescript { .api }
54
/**
55
* Get the root directory for a given project type
56
* @param projectType - The type of project
57
* @returns Directory name for the project type
58
*/
59
function projectRootDir(projectType: ProjectType): string;
60
```
61
62
**Usage Examples:**
63
64
```typescript
65
import { ProjectType, projectRootDir } from "@nx/workspace";
66
67
// Get standard directories
68
const appsDir = projectRootDir(ProjectType.Application);
69
console.log(appsDir); // "apps"
70
71
const libsDir = projectRootDir(ProjectType.Library);
72
console.log(libsDir); // "libs"
73
74
// Use in project generation
75
function generateProject(name: string, type: ProjectType) {
76
const rootDir = projectRootDir(type);
77
const projectPath = `${rootDir}/${name}`;
78
79
console.log(`Creating ${type} at ${projectPath}`);
80
// Project creation logic here
81
}
82
83
// Generate different project types
84
generateProject("my-app", ProjectType.Application); // Creates in apps/my-app
85
generateProject("shared-ui", ProjectType.Library); // Creates in libs/shared-ui
86
```
87
88
## Workspace Organization
89
90
### Standard Directory Structure
91
92
Nx workspaces typically organize projects using these conventions:
93
94
```
95
workspace-root/
96
├── apps/ # Applications (ProjectType.Application)
97
│ ├── web-app/ # Frontend application
98
│ ├── api/ # Backend API application
99
│ └── mobile-app/ # Mobile application
100
├── libs/ # Libraries (ProjectType.Library)
101
│ ├── shared/ # Shared utilities
102
│ │ ├── ui/ # UI component library
103
│ │ ├── data-access/ # Data access layer
104
│ │ └── utils/ # Utility functions
105
│ └── feature/ # Feature-specific libraries
106
│ ├── auth/ # Authentication feature
107
│ └── dashboard/ # Dashboard feature
108
└── tools/ # Development tools and scripts
109
```
110
111
### Project Type Characteristics
112
113
**Applications (ProjectType.Application):**
114
- Executable projects that can be served, built, or deployed
115
- Located in `apps/` directory by default
116
- Examples: web apps, APIs, mobile apps, desktop applications
117
- Can have build, serve, test, lint, and e2e targets
118
- May depend on libraries but libraries should not depend on applications
119
120
**Libraries (ProjectType.Library):**
121
- Reusable code packages that can be imported by other projects
122
- Located in `libs/` directory by default
123
- Examples: UI components, utilities, data access layers, business logic
124
- Can have build, test, and lint targets
125
- Can depend on other libraries and be depended upon by applications or other libraries
126
127
## Advanced Usage
128
129
### Custom Project Organization
130
131
```typescript
132
import { ProjectType, projectRootDir } from "@nx/workspace";
133
134
function getProjectPath(name: string, type: ProjectType, scope?: string): string {
135
const rootDir = projectRootDir(type);
136
137
if (scope) {
138
return `${rootDir}/${scope}/${name}`;
139
}
140
141
return `${rootDir}/${name}`;
142
}
143
144
// Scoped project organization
145
const uiLibPath = getProjectPath("button", ProjectType.Library, "shared");
146
console.log(uiLibPath); // "libs/shared/button"
147
148
const featureLibPath = getProjectPath("user-management", ProjectType.Library, "feature");
149
console.log(featureLibPath); // "libs/feature/user-management"
150
```
151
152
### Type Guards and Validation
153
154
```typescript
155
import { ProjectType } from "@nx/workspace";
156
157
function isApplicationProject(type: string): type is ProjectType.Application {
158
return type === ProjectType.Application;
159
}
160
161
function isLibraryProject(type: string): type is ProjectType.Library {
162
return type === ProjectType.Library;
163
}
164
165
// Validation function
166
function validateProjectType(type: string): ProjectType {
167
if (isApplicationProject(type) || isLibraryProject(type)) {
168
return type as ProjectType;
169
}
170
throw new Error(`Invalid project type: ${type}`);
171
}
172
173
// Usage in generators
174
function createProject(name: string, type: string) {
175
const validatedType = validateProjectType(type);
176
const directory = projectRootDir(validatedType);
177
178
if (isApplicationProject(validatedType)) {
179
// Application-specific setup
180
console.log(`Setting up application in ${directory}/${name}`);
181
} else {
182
// Library-specific setup
183
console.log(`Setting up library in ${directory}/${name}`);
184
}
185
}
186
```
187
188
## Integration with Nx Devkit
189
190
These utilities integrate seamlessly with Nx Devkit functions:
191
192
```typescript
193
import { Tree, addProjectConfiguration } from "@nx/devkit";
194
import { ProjectType, projectRootDir } from "@nx/workspace";
195
196
function addNxProject(
197
tree: Tree,
198
name: string,
199
type: ProjectType,
200
options: any = {}
201
) {
202
const projectRoot = `${projectRootDir(type)}/${name}`;
203
204
addProjectConfiguration(tree, name, {
205
root: projectRoot,
206
projectType: type,
207
sourceRoot: `${projectRoot}/src`,
208
targets: {
209
build: {
210
executor: type === ProjectType.Application
211
? "@nx/webpack:webpack"
212
: "@nx/js:tsc"
213
},
214
test: {
215
executor: "@nx/jest:jest"
216
}
217
},
218
...options
219
});
220
}
221
```
222
223
## Types
224
225
```typescript { .api }
226
enum ProjectType {
227
Application = 'application',
228
Library = 'library'
229
}
230
231
type ProjectTypeString = 'application' | 'library';
232
```