NestJSInterview preparation
Foundation·Concept·2 min read

What are custom providers and provider scopes?

Practice this question

Definition:

A provider is a dependency that Nest can supply, such as a class instance, value or factory result. Custom providers control how that dependency is created, and scope controls how long its instances are shared. These choices affect testability, state isolation and allocation cost.

Key points:

  • useValue: supply a value; useClass: supply a class; useFactory: build a dependency.

  • Singleton is the default; request scope creates an instance per request.

  • Transient scope gives each consumer its own instance; extra instances add overhead.

  • Do not store one user's request data on a singleton service field; concurrent requests can observe shared state.

Interview point:

  • Choose request scope only when lifecycle requirements justify it; singleton is not a license to keep request-specific mutable state.
Adapted from 03_NESTJS.md · “What are custom providers and provider scopes?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.