🦀 ClawHub
NestJS
by @ivangdavila
Avoid common NestJS mistakes — DI scoping, circular dependencies, validation pipes, and module organization traps.
TERMINAL
clawhub install nestjs📖 About This Skill
name: NestJS description: Avoid common NestJS mistakes — DI scoping, circular dependencies, validation pipes, and module organization traps. metadata: {"clawdbot":{"emoji":"🐱","requires":{"bins":["node"]},"os":["linux","darwin","win32"]}}
Dependency Injection
providers array AND exports if used by other modulesforwardRef(() => Module) in both modules@Injectable({ scope: Scope.REQUEST }), propagates to dependentsModule Organization
imports: [UserModule] not providers: [UserService]exports makes providers available to importers — without it, provider stays private@Global() decorator — only for truly shared (config, logger)forRoot() vs forRootAsync() — async for when config depends on other providersValidation
ValidationPipe needs class-validator decorators — plain classes won't validatetransform: true for auto-transformation — string "1" to number 1whitelist: true strips unknown properties — forbidNonWhitelisted: true to error instead@ValidateNested() AND @Type(() => NestedDto) — both requiredExecution Order
Exception Handling
throw new HttpException() not return — must throw for filter to catchHttpException — or implement ExceptionFilterBadRequestException, NotFoundException, etc. — use these, not generic HttpExceptionTesting
createTestingModule doesn't auto-mock — provide mocks explicitly in providers.overrideProvider(X).useValue(mock) — before .compile()app.init() — and app.close() in afterAllCommon Mistakes
@Body() without DTO returns plain object — no validation, no transformation@Param('id') is always string — use ParseIntPipe for number: @Param('id', ParseIntPipe)useFactory: async () => await createConnection()await on async service methods — returns Promise, not value🔒 Constraints
ValidationPipe needs class-validator decorators — plain classes won't validatetransform: true for auto-transformation — string "1" to number 1whitelist: true strips unknown properties — forbidNonWhitelisted: true to error instead@ValidateNested() AND @Type(() => NestedDto) — both required