A concise list of NestJS do's and don'ts to reinforce correct patterns and avoid common production footguns.
Do These Things
Do register providers in modules. Do validate DTOs. Do inject dependencies via constructors. Do keep business logic in services. Do use ConfigService for secrets. Do write tests for critical flows.
// DO
constructor(private readonly usersService: UsersService) {}
// DON'T
const usersService = new UsersService(new UsersRepository());
Manual new bypasses Nest DI, lifecycle hooks, and easy mocking.
Don't Do These Things
- Don't put DB queries in controllers
- Don't trust client input without ValidationPipe
- Don't commit secrets
- Don't use synchronize:true in production
- Don't ignore shutdown hooks in containers
- Don't catch and swallow errors silently
Do prefer explicit imports/exports over hidden globals.