| name | nestjs-expert |
| type | reference |
| description | Provides NestJS patterns for modules, controllers, providers, guards, interceptors, and microservices. Use when working with NestJS TypeScript files (*.module.ts, *.controller.ts, *.service.ts) or when the user mentions NestJS, Nest.js, or NestJS modules. |
| paths | ["**/*.module.ts","**/*.controller.ts","**/*.service.ts","**/*.guard.ts","**/nest-cli.json"] |
| effort | 3 |
| allowed-tools | Read, Glob, Grep, Write, Edit, Bash |
| user-invocable | true |
| when_to_use | When building NestJS backend APIs, microservices, or guards/interceptors |
NestJS Expert
Critical rules (non-obvious)
- Circular dependencies: use
forwardRef(() => ServiceB) in both modules; better — restructure to avoid
- Global modules: use
@Global() sparingly; prefer explicit imports to keep modules testable
APP_GUARD / APP_INTERCEPTOR: registered in AppModule providers, not in individual modules
- Lifecycle hooks order:
onModuleInit → onApplicationBootstrap → ready; onModuleDestroy → beforeApplicationShutdown → onApplicationShutdown
- Never use
req.user without type assertion — it's any from Passport; extend Express.Request
Module structure
@Module({
imports: [TypeOrmModule.forFeature([User]), JwtModule],
controllers: [UserController],
providers: [UserService, UserRepository],
exports: [UserService],
})
export class UserModule {}
Controller with validation
@Controller("users")
@UseGuards(JwtAuthGuard)
export class UserController {
constructor(private readonly userService: ) {}
()
(.)
() {
..(id);
}
()
(.)
() {
..(dto);
}
}