Overview - Provider scope (default, request, transient)
What is it?
In NestJS, provider scope controls how and when instances of services or classes are created and shared. There are three main scopes: default (singleton), request, and transient. Default scope means one instance is shared across the whole app. Request scope creates a new instance for each incoming request. Transient scope creates a new instance every time the provider is injected.
Why it matters
Provider scopes help manage resource usage and data isolation in applications. Without scopes, all services would be singletons, which can cause bugs when state is shared unexpectedly or when you need fresh data per request. Scopes allow developers to control lifecycle and memory, improving app reliability and performance.
Where it fits
Before learning provider scopes, you should understand basic NestJS providers and dependency injection. After mastering scopes, you can explore advanced topics like custom providers, lifecycle hooks, and request context management.