NestJS
Installation
SKILL.md
Dependency Injection
- Provider not available — must be in
providersarray ANDexportsif used by other modules - Circular dependency crashes — use
forwardRef(() => Module)in both modules - Default scope is singleton — same instance across requests, careful with state
- Request-scoped provider —
@Injectable({ scope: Scope.REQUEST }), propagates to dependents
Module Organization
- Import module, not provider directly —
imports: [UserModule]notproviders: [UserService] exportsmakes providers available to importers — without it, provider stays private- Global modules need
@Global()decorator — only for truly shared (config, logger) forRoot()vsforRootAsync()— async for when config depends on other providers
Validation
ValidationPipeneedsclass-validatordecorators — plain classes won't validate- Enable
transform: truefor auto-transformation — string"1"to number1 whitelist: truestrips unknown properties —forbidNonWhitelisted: trueto error instead- Nested objects need
@ValidateNested()AND@Type(() => NestedDto)— both required