0
# Library Integration
1
2
Add external libraries to your NestJS project with automated setup, configuration, and integration. The add command streamlines the process of incorporating third-party packages and NestJS-specific libraries into your application.
3
4
## Capabilities
5
6
### Add Command
7
8
Integrates external libraries with automated setup and configuration management.
9
10
```bash { .api }
11
# Add library to project
12
nest add <library> [options] [library-specific-options]
13
14
# Options:
15
-d, --dry-run # Report actions without writing results
16
-s, --skip-install # Skip package installation (default: false)
17
-p, --project [project] # Project in which to generate files
18
[library-specific-options] # Additional options passed to the library's schematic
19
```
20
21
**Usage Examples:**
22
23
```bash
24
# Add Swagger/OpenAPI support
25
nest add @nestjs/swagger
26
27
# Add GraphQL support
28
nest add @nestjs/graphql
29
30
# Add database integration
31
nest add @nestjs/typeorm
32
nest add @nestjs/mongoose
33
34
# Add authentication
35
nest add @nestjs/passport
36
37
# Preview changes without installing
38
nest add @nestjs/swagger --dry-run
39
40
# Add to specific project in monorepo
41
nest add @nestjs/swagger --project api-gateway
42
43
# Skip automatic package installation
44
nest add @nestjs/swagger --skip-install
45
```
46
47
## Supported Libraries
48
49
### Core NestJS Libraries
50
51
Official NestJS packages with automated integration:
52
53
```bash { .api }
54
# Web Framework Enhancements
55
nest add @nestjs/swagger # OpenAPI/Swagger documentation
56
nest add @nestjs/graphql # GraphQL API development
57
nest add @nestjs/websockets # WebSocket support
58
nest add @nestjs/microservices # Microservices architecture
59
60
# Database Integration
61
nest add @nestjs/typeorm # TypeORM database integration
62
nest add @nestjs/mongoose # MongoDB with Mongoose
63
nest add @nestjs/sequelize # Sequelize ORM
64
nest add @nestjs/prisma # Prisma database toolkit
65
66
# Authentication & Security
67
nest add @nestjs/passport # Passport authentication
68
nest add @nestjs/jwt # JSON Web Token support
69
nest add @nestjs/throttler # Rate limiting
70
71
# Caching & Performance
72
nest add @nestjs/cache-manager # Caching solutions
73
nest add @nestjs/bull # Queue management
74
nest add @nestjs/schedule # Task scheduling
75
76
# Monitoring & Logging
77
nest add @nestjs/terminus # Health checks
78
nest add @nestjs/logger # Enhanced logging
79
80
# Configuration
81
nest add @nestjs/config # Configuration management
82
nest add @nestjs/serve-static # Static file serving
83
```
84
85
### Third-Party Libraries
86
87
Many popular Node.js libraries with NestJS schematics:
88
89
```bash { .api }
90
# State Management
91
nest add @ngrx/store # Redux-style state management
92
93
# Validation
94
nest add class-validator # Decorator-based validation
95
nest add joi # Schema validation
96
97
# Testing
98
nest add @nestjs/testing # Enhanced testing utilities
99
100
# Development Tools
101
nest add @nestjs/devtools-integration # NestJS DevTools
102
```
103
104
## Library-Specific Options
105
106
Many libraries accept additional configuration options during installation:
107
108
### Swagger Integration
109
110
```bash { .api }
111
# Basic Swagger setup
112
nest add @nestjs/swagger
113
114
# With custom configuration
115
nest add @nestjs/swagger --project api --title "My API" --version "1.0"
116
```
117
118
### GraphQL Setup
119
120
```bash { .api }
121
# Apollo GraphQL (default)
122
nest add @nestjs/graphql
123
124
# With schema-first approach
125
nest add @nestjs/graphql --schemaFirst
126
127
# With custom configuration
128
nest add @nestjs/graphql --playground --introspection
129
```
130
131
### TypeORM Database
132
133
```bash { .api }
134
# Basic TypeORM setup
135
nest add @nestjs/typeorm
136
137
# With specific database
138
nest add @nestjs/typeorm --database postgres
139
nest add @nestjs/typeorm --database mysql
140
nest add @nestjs/typeorm --database sqlite
141
```
142
143
## Installation Process
144
145
### Automated Steps
146
147
When adding a library, the CLI typically performs:
148
149
1. **Package Installation**: Downloads and installs the library and its dependencies
150
2. **Code Generation**: Creates configuration files and boilerplate code
151
3. **Module Registration**: Updates app.module.ts or creates feature modules
152
4. **Configuration Setup**: Adds necessary configuration files
153
5. **Import Updates**: Updates import statements and module dependencies
154
155
### Example: Adding Swagger
156
157
```bash
158
nest add @nestjs/swagger
159
```
160
161
**Generated Changes:**
162
- Installs `@nestjs/swagger` and `swagger-ui-express`
163
- Updates `main.ts` with Swagger configuration
164
- Adds OpenAPI decorators to controllers
165
- Creates Swagger documentation endpoint at `/api`
166
167
### Example: Adding TypeORM
168
169
```bash
170
nest add @nestjs/typeorm
171
```
172
173
**Generated Changes:**
174
- Installs `@nestjs/typeorm`, `typeorm`, and database driver
175
- Creates `ormconfig.json` or database configuration
176
- Updates `app.module.ts` with TypeORM module
177
- Generates entity and repository examples
178
- Sets up database connection configuration
179
180
## Project Structure Impact
181
182
### Configuration Files
183
184
Added libraries often create configuration files:
185
186
```
187
project-root/
188
├── ormconfig.json # TypeORM configuration
189
├── .env # Environment variables
190
├── swagger-config.json # Swagger configuration
191
└── src/
192
├── config/ # Configuration modules
193
├── entities/ # Database entities
194
└── modules/ # Feature modules
195
```
196
197
### Module Structure
198
199
Libraries typically integrate into the module system:
200
201
```typescript
202
// app.module.ts after adding libraries
203
@Module({
204
imports: [
205
ConfigModule.forRoot(),
206
TypeOrmModule.forRoot({
207
// Database configuration
208
}),
209
GraphQLModule.forRoot({
210
// GraphQL configuration
211
}),
212
// Other modules...
213
],
214
})
215
export class AppModule {}
216
```
217
218
## Monorepo Support
219
220
### Project-Specific Installation
221
222
```bash { .api }
223
# Add to specific application
224
nest add @nestjs/swagger --project api-gateway
225
nest add @nestjs/typeorm --project user-service
226
227
# Add to shared library
228
nest add @nestjs/common --project shared-utils
229
```
230
231
### Cross-Project Dependencies
232
233
Some libraries can be shared across monorepo projects:
234
235
```bash
236
# Add shared authentication library
237
nest add @nestjs/passport --project auth-lib
238
239
# Add project-specific database connection
240
nest add @nestjs/typeorm --project user-service
241
```
242
243
## Advanced Integration
244
245
### Custom Schematics
246
247
Some libraries provide custom schematics for advanced setup:
248
249
```bash { .api }
250
# Run specific library schematics
251
nest add @my-org/custom-lib --schematic advanced-setup
252
nest add @nestjs/graphql --schematic federation
253
```
254
255
### Configuration Options
256
257
Pass configuration directly during installation:
258
259
```bash { .api }
260
# Complex configuration
261
nest add @nestjs/typeorm \
262
--database postgres \
263
--host localhost \
264
--port 5432 \
265
--synchronize false
266
```
267
268
## Dry Run and Validation
269
270
### Preview Changes
271
272
```bash { .api }
273
# See what would be changed
274
nest add @nestjs/swagger --dry-run
275
```
276
277
**Dry Run Output:**
278
- Lists files that would be created
279
- Shows files that would be modified
280
- Displays packages that would be installed
281
- Previews configuration changes
282
283
### Manual Installation
284
285
```bash { .api }
286
# Skip automatic package installation
287
nest add @nestjs/swagger --skip-install
288
289
# Then manually install
290
npm install @nestjs/swagger swagger-ui-express
291
```
292
293
## Common Integration Patterns
294
295
### API Documentation
296
297
```bash { .api }
298
# Complete API documentation setup
299
nest add @nestjs/swagger
300
nest add @nestjs/config # For environment-based configuration
301
```
302
303
### Database with Auth
304
305
```bash { .api }
306
# Full backend setup
307
nest add @nestjs/typeorm
308
nest add @nestjs/passport
309
nest add @nestjs/jwt
310
nest add @nestjs/config
311
```
312
313
### GraphQL API
314
315
```bash { .api }
316
# GraphQL with database
317
nest add @nestjs/graphql
318
nest add @nestjs/typeorm
319
nest add apollo-server-express
320
```
321
322
## Error Handling
323
324
### Invalid Library Names
325
326
```bash
327
# Unknown library
328
nest add unknown-library
329
# Error: Schematic "ng-add" not found in collection "unknown-library"
330
```
331
332
### Dependency Conflicts
333
334
The CLI detects and reports dependency conflicts:
335
- Version incompatibilities
336
- Peer dependency requirements
337
- Duplicate installations
338
339
### Installation Failures
340
341
If package installation fails, the CLI:
342
- Reports the specific error
343
- Suggests manual installation steps
344
- Provides rollback instructions
345
- Preserves existing project state