0
# Application Building
1
2
Build NestJS applications for production with support for multiple build systems, watch mode, and advanced compilation options. The CLI supports TypeScript compiler, Webpack, and SWC for different performance and feature requirements.
3
4
## Capabilities
5
6
### Build Command
7
8
Compiles your NestJS application with comprehensive configuration options and multiple builder support.
9
10
```bash { .api }
11
# Build applications
12
nest build [apps...] [options]
13
14
# Options:
15
-c, --config [path] # Path to nest-cli configuration file
16
-p, --path [path] # Path to tsconfig file
17
-w, --watch # Run in watch mode (live-reload)
18
-b, --builder [name] # Builder to be used (tsc, webpack, swc)
19
--watchAssets # Watch non-ts (e.g., .graphql) files mode
20
--webpack # Use webpack for compilation (deprecated, use --builder)
21
--type-check # Enable type checking (when SWC is used)
22
--webpackPath [path] # Path to webpack configuration
23
--tsc # Use typescript compiler for compilation
24
--preserveWatchOutput # Use "preserveWatchOutput" option when using tsc watch mode
25
--all # Build all projects in a monorepo
26
```
27
28
**Usage Examples:**
29
30
```bash
31
# Basic build
32
nest build
33
34
# Build specific app in monorepo
35
nest build api-gateway
36
37
# Build with watch mode
38
nest build --watch
39
40
# Build using specific builder
41
nest build --builder webpack
42
nest build --builder swc
43
nest build --builder tsc
44
45
# Build with asset watching
46
nest build --watch --watchAssets
47
48
# Build all projects in monorepo
49
nest build --all
50
51
# Build with custom config file
52
nest build --config nest-cli.production.json
53
54
# Build with custom TypeScript config
55
nest build --path tsconfig.production.json
56
```
57
58
## Build Systems
59
60
### TypeScript Compiler (tsc)
61
62
The default TypeScript compiler provides standard compilation with full type checking.
63
64
```bash { .api }
65
# Use TypeScript compiler explicitly
66
nest build --builder tsc
67
nest build --tsc
68
69
# With watch mode and preserved output
70
nest build --tsc --watch --preserveWatchOutput
71
```
72
73
**Features:**
74
- Full type checking
75
- Standard TypeScript compilation
76
- Source map generation
77
- Declaration file generation
78
- Incremental compilation support
79
80
### Webpack
81
82
Advanced bundling and optimization with hot module replacement support for development.
83
84
```bash { .api }
85
# Use Webpack builder
86
nest build --builder webpack
87
88
# With custom webpack config
89
nest build --builder webpack --webpackPath webpack.config.js
90
91
# Watch mode with Webpack
92
nest build --builder webpack --watch
93
```
94
95
**Features:**
96
- Module bundling and optimization
97
- Hot module replacement (HMR)
98
- Advanced code splitting
99
- Asset optimization
100
- Development server integration
101
102
### SWC
103
104
High-performance Rust-based compiler for faster builds with optional type checking.
105
106
```bash { .api }
107
# Use SWC builder
108
nest build --builder swc
109
110
# SWC with type checking enabled
111
nest build --builder swc --type-check
112
113
# SWC in watch mode
114
nest build --builder swc --watch
115
```
116
117
**Features:**
118
- Extremely fast compilation
119
- TypeScript and JavaScript support
120
- Optional type checking
121
- Modern JavaScript output
122
- Requires @swc/cli and @swc/core dependencies
123
124
## Watch Mode
125
126
Enable automatic rebuilding when files change during development.
127
128
```bash { .api }
129
# Basic watch mode
130
nest build --watch
131
132
# Watch mode with asset monitoring
133
nest build --watch --watchAssets
134
135
# Watch mode with preserved output (tsc only)
136
nest build --watch --preserveWatchOutput
137
```
138
139
### Asset Watching
140
141
Monitor and rebuild when non-TypeScript files change:
142
143
```bash { .api }
144
# Watch for changes in .graphql, .json, .proto files
145
nest build --watch --watchAssets
146
```
147
148
Supported asset types:
149
- GraphQL schema files (.graphql, .gql)
150
- JSON configuration files
151
- Protocol buffer definitions (.proto)
152
- Template files
153
- Static assets
154
155
## Monorepo Support
156
157
### Build Specific Apps
158
159
```bash { .api }
160
# Build individual applications
161
nest build api-gateway
162
nest build user-service
163
nest build notification-service
164
165
# Build multiple specific apps
166
nest build api-gateway user-service
167
```
168
169
### Build All Projects
170
171
```bash { .api }
172
# Build entire monorepo
173
nest build --all
174
```
175
176
## Configuration Files
177
178
### nest-cli.json
179
180
Project-level build configuration:
181
182
```json
183
{
184
"collection": "@nestjs/schematics",
185
"sourceRoot": "src",
186
"compilerOptions": {
187
"builder": "webpack",
188
"typeCheck": true,
189
"assets": ["**/*.proto", "**/*.graphql"],
190
"watchAssets": true
191
}
192
}
193
```
194
195
### tsconfig.json Integration
196
197
The build command respects TypeScript configuration:
198
199
```bash { .api }
200
# Use custom tsconfig file
201
nest build --path tsconfig.production.json
202
```
203
204
## Build Output
205
206
### Default Output Structure
207
208
```
209
dist/
210
├── main.js
211
├── main.js.map
212
└── [other compiled files]
213
```
214
215
### Monorepo Output Structure
216
217
```
218
dist/
219
├── apps/
220
│ ├── api-gateway/
221
│ └── user-service/
222
└── libs/
223
├── shared/
224
└── common/
225
```
226
227
## Builder Selection
228
229
The CLI automatically selects the appropriate builder based on:
230
231
1. Explicit `--builder` flag
232
2. `nest-cli.json` configuration
233
3. Available dependencies
234
4. Project type and structure
235
236
### Builder Priority
237
238
```bash { .api }
239
# Explicit builder flag (highest priority)
240
nest build --builder swc
241
242
# nest-cli.json compilerOptions.builder
243
# Available dependencies (webpack, swc, tsc)
244
# Default fallback to tsc
245
```
246
247
## Error Handling
248
249
### Invalid Builder
250
251
```bash
252
# Invalid builder name
253
nest build --builder invalid
254
# Error: Invalid builder option: invalid. Available builders: tsc, webpack, swc
255
```
256
257
### Missing Dependencies
258
259
When using SWC or Webpack without required dependencies, the CLI provides clear installation instructions.
260
261
## Performance Optimization
262
263
### SWC for Speed
264
265
```bash { .api }
266
# Fastest builds
267
nest build --builder swc
268
269
# Fast builds with type safety
270
nest build --builder swc --type-check
271
```
272
273
### Webpack for Features
274
275
```bash { .api }
276
# Advanced optimization and bundling
277
nest build --builder webpack
278
279
# Development with HMR
280
nest build --builder webpack --watch
281
```
282
283
### TypeScript for Compatibility
284
285
```bash { .api }
286
# Full TypeScript features and compatibility
287
nest build --builder tsc
288
289
# Incremental builds
290
nest build --builder tsc --watch
291
```