NestJS Best Practices
A condensed summary of the 25 most important NestJS practices for Node.js teams - drawn from every page in this section.
-
Use NestJS 11 on Node 24 LTS: Current major with Express 5 and Fastify 5 support - NestJS Basics.
-
Fastify adapter by default: 20-40% faster than Express adapter - NestJS Performance Reality.
-
One module per domain: UsersModule, OrdersModule, BillingModule. No god AppModule providers.
-
Export only what other modules need: Keep providers private by default.
-
Thin controllers: Delegate to services; controllers only handle HTTP concerns.
-
Injectable services for business logic: Testable, DI-wired, single responsibility.
-
Global ValidationPipe with whitelist: Strip unknown properties from DTOs.
-
DTOs with class-validator decorators: Every POST/PUT body typed and validated.
-
Guards for authentication: Not middleware, not interceptor - Guards, Interceptors & Pipes.
-
@Public() decorator for health/auth routes: K8s probes must not require tokens.
-
Selective interceptors: Not five global interceptors on every route.
-
PrismaService with lifecycle hooks:
$connecton init,$disconnecton destroy - NestJS + Prisma/TypeORM. -
Singleton providers by default: Request scope only for request-specific state.
-
Custom provider tokens for interfaces:
Symbol+useClassfor swappable implementations. -
overrideProvider in tests: Mock database, email, and external clients.
-
ConfigModule global with validation: Joi or Zod schema on env at startup.
-
Global exception filter: Consistent error JSON across all endpoints.
-
setGlobalPrefix with health exclusion:
/api/v1for routes,/healthbare. -
Avoid circular dependencies: Refactor before
forwardRefeverywhere. -
Message pattern versioning for microservices:
order.create.v2- Microservices Mode. -
Hybrid HTTP + microservice for health: Keep HTTP health even with TCP transport.
-
Profile before blaming NestJS: Database is usually the bottleneck.
-
Return plain objects from services: Avoid class-transformer overhead on responses.
-
CLI for scaffolding:
nest g module,nest g controller,nest g service. -
Don't use NestJS for 5-endpoint APIs: Framework weight must match complexity.
FAQs
When is NestJS the right choice?
Modular monoliths, teams wanting Spring-like structure, microservices with multiple transporters, and projects with 30+ endpoints.
Express or Fastify adapter?
Fastify unless a critical dependency requires Express. Performance difference is real.
Prisma or TypeORM?
Prisma for DX and migrations. TypeORM for decorator entities and existing TypeORM codebases. Pick one.
How do I prevent god modules?
Max 2 controllers and 3-5 providers per feature module. Split when a module file exceeds 200 lines.
Should I use microservices mode?
Only when you need event-driven or RPC communication between services. HTTP REST is simpler for most cases.
Related
- NestJS Basics - getting started
- Dependency Injection - DI patterns
- Fastify Best Practices - underlying adapter
- Framework Selection Checklist - framework ADR
- Modularity Best Practices - code organization
Stack versions: This page was written for Node.js 24.18.0 (Active LTS), npm 10+, TypeScript 5.6+, Express 5, Fastify 5, and NestJS 11.