๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ
ๅฐ้พ™่™พๅฐ้พ™่™พAI
๐Ÿค–

NestJS

Avoid common NestJS mistakes โ€” DI scoping, circular dependencies, validation pipes, and module organization traps.

ไธ‹่ฝฝ1.7k
ๆ˜Ÿๆ ‡2
็‰ˆๆœฌ1.0.0
ๅ…ถไป–
ๅฎ‰ๅ…จ้€š่ฟ‡
๐Ÿ’ฌPrompt

ๆŠ€่ƒฝ่ฏดๆ˜Ž


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

  • Provider not available โ€” must be in providers array AND exports if 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] not providers: [UserService]
  • exports makes providers available to importers โ€” without it, provider stays private
  • Global modules need @Global() decorator โ€” only for truly shared (config, logger)
  • forRoot() vs forRootAsync() โ€” async for when config depends on other providers

Validation

  • ValidationPipe needs class-validator decorators โ€” plain classes won't validate
  • Enable transform: true for auto-transformation โ€” string "1" to number 1
  • whitelist: true strips unknown properties โ€” forbidNonWhitelisted: true to error instead
  • Nested objects need @ValidateNested() AND @Type(() => NestedDto) โ€” both required

Execution Order

  • Middleware โ†’ Guards โ†’ Interceptors (pre) โ†’ Pipes โ†’ Handler โ†’ Interceptors (post) โ†’ Filters
  • Guards can't access transformed body โ€” run before pipes
  • Global pipes run before route pipes โ€” but after guards
  • Exception filters catch errors from entire chain โ€” including guards and pipes

Exception Handling

  • throw new HttpException() not return โ€” must throw for filter to catch
  • Custom exceptions extend HttpException โ€” or implement ExceptionFilter
  • Unhandled exceptions become 500 โ€” wrap external calls in try/catch
  • Built-in exceptions: BadRequestException, NotFoundException, etc. โ€” use these, not generic HttpException

Testing

  • createTestingModule doesn't auto-mock โ€” provide mocks explicitly in providers
  • Override with .overrideProvider(X).useValue(mock) โ€” before .compile()
  • E2E tests need app.init() โ€” and app.close() in afterAll
  • Request-scoped providers complicate unit tests โ€” consider making them singleton when possible

Common Mistakes

  • @Body() without DTO returns plain object โ€” no validation, no transformation
  • @Param('id') is always string โ€” use ParseIntPipe for number: @Param('id', ParseIntPipe)
  • Guards returning false gives 403 โ€” throw specific exception for better error messages
  • Async providers need factory โ€” useFactory: async () => await createConnection()
  • Forgetting await on async service methods โ€” returns Promise, not value

ๅฆ‚ไฝ•ไฝฟ็”จใ€ŒNestJSใ€๏ผŸ

  1. ๆ‰“ๅผ€ๅฐ้พ™่™พAI๏ผˆWeb ๆˆ– iOS App๏ผ‰
  2. ็‚นๅ‡ปไธŠๆ–นใ€Œ็ซ‹ๅณไฝฟ็”จใ€ๆŒ‰้’ฎ๏ผŒๆˆ–ๅœจๅฏน่ฏๆก†ไธญ่พ“ๅ…ฅไปปๅŠกๆ่ฟฐ
  3. ๅฐ้พ™่™พAI ไผš่‡ชๅŠจๅŒน้…ๅนถ่ฐƒ็”จใ€ŒNestJSใ€ๆŠ€่ƒฝๅฎŒๆˆไปปๅŠก
  4. ็ป“ๆžœๅณๆ—ถๅ‘ˆ็Žฐ๏ผŒๆ”ฏๆŒ็ปง็ปญๅฏน่ฏไผ˜ๅŒ–

็›ธๅ…ณๆŠ€่ƒฝ