Skip to content

Common NestJS Mistakes

Even experienced Node developers hit Nest-specific pitfalls. This lesson catalogs frequent mistakes and the fixes.

Mistake: Provider Not Found

The classic runtime error: Nest cannot resolve a dependency. Usually the provider was never listed in providers, or a module forgot to export/import it.

// Fix: export from owning module and import that module where needed
@Module({ providers: [UsersService], exports: [UsersService] })
export class UsersModule {}

@Module({ imports: [UsersModule] })
export class OrdersModule {}

Importing a module does not automatically re-export its providers—only exports makes them available.

Other Frequent Mistakes

- Forgetting ValidationPipe (DTOs unchecked at runtime)
- Circular dependencies without redesign/forwardRef
- Returning ORM entities with password hashes
- Using request scope everywhere "just in case"
- Catching errors and returning 200 anyway
- Not awaiting Promises in interceptors/guards
  • Read Nest dependency error stacks carefully—they name the missing token.
  • Enable transform + whitelist on ValidationPipe early.
  • Prefer redesign over cascading forwardRef usage.
  • Map entities to response DTOs before returning.

Mistakes Cheatsheet

Symptom -> Likely cause.

Symptom Likely Cause
Nest can't resolve dependencies Missing provider/export/import
req.body fields always pass No ValidationPipe
undefined config secrets Env not loaded/validated
Tests hang App not closed / open handles
Random auth failures Clock skew / wrong JWT secret

Unhandled Async Errors

Always return/await Promises from guards, interceptors, and pipes. Floating promises can crash processes or skip Nest filters.

Leaky Module Boundaries

If every module imports everything, you lose the benefit of Nest modularity. Audit exports and keep dependencies intentional.

Common Mistakes

  • Ignoring framework errors instead of fixing wiring.
  • Copy-pasting modules without updating provider arrays.
  • Shipping debug endpoints protected by nothing.

Key Takeaways

  • Most Nest bugs are wiring/validation issues, not Node mysteries.
  • Export/import providers deliberately.
  • Validate input and await async boundaries.
  • Map safe response DTOs instead of leaking entities.

Pro Tip

When you see a DI error, check providers -> exports -> imports in that order before refactoring business code.