Provider scope controls how NestJS creates and shares service instances. It helps manage when and how often a service is created.
Provider scope (default, request, transient) in NestJS
import { Injectable, Scope } from '@nestjs/common'; @Injectable({ scope: Scope.DEFAULT }) export class MyService {}
Use Scope.DEFAULT for a singleton service shared app-wide.
Use Scope.REQUEST to create a new instance per HTTP request.
import { Injectable, Scope } from '@nestjs/common'; @Injectable() export class DefaultService {}
import { Injectable, Scope } from '@nestjs/common'; @Injectable({ scope: Scope.REQUEST }) export class RequestService {}
import { Injectable, Scope } from '@nestjs/common'; @Injectable({ scope: Scope.TRANSIENT }) export class TransientService {}
This example shows a request-scoped service that generates a random ID when created. Each HTTP request gets a new service instance with a different ID.
import { Injectable, Scope, Controller, Get } from '@nestjs/common'; @Injectable({ scope: Scope.REQUEST }) export class RequestScopedService { private readonly id: number; constructor() { this.id = Math.floor(Math.random() * 1000); } getId() { return this.id; } } @Controller() export class AppController { constructor(private readonly requestService: RequestScopedService) {} @Get() getId() { return `RequestScopedService ID: ${this.requestService.getId()}`; } }
Default scope means one instance shared everywhere.
Request scope is useful for services that hold request-specific data.
Transient scope creates a fresh instance every time, useful for stateless helpers.
Provider scope controls service instance lifetime in NestJS.
Default scope shares one instance app-wide.
Request scope creates a new instance per HTTP request.
Transient scope creates a new instance every injection.
Practice
Solution
Step 1: Understand default provider scope behavior
By default, NestJS creates one instance of a provider and shares it across the whole app.Step 2: Compare with other scopes
Request scope creates per HTTP request instances, transient creates per injection instances, so default is the shared singleton.Final Answer:
A single instance is shared across the entire application. -> Option BQuick Check:
Default scope = singleton instance [OK]
- Confusing default with request scope
- Thinking default creates new instances per injection
- Assuming default destroys instance after use
Solution
Step 1: Recall correct syntax for setting scope
In NestJS, the scope is set using the Scope enum inside @Injectable decorator like { scope: Scope.TRANSIENT }.Step 2: Eliminate incorrect options
@Injectable({ scope: Scope.REQUEST }) uses REQUEST scope, @Injectable({ scope: 'transient' }) uses string instead of enum, @Injectable({ transient: true }) uses invalid property.Final Answer:
@Injectable({ scope: Scope.TRANSIENT }) -> Option CQuick Check:
Use Scope.TRANSIENT enum for transient scope [OK]
- Using string 'transient' instead of Scope enum
- Confusing transient with request scope
- Using invalid decorator properties
@Injectable({ scope: Scope.TRANSIENT })
export class MyService {
id = Math.random();
}
If this service is injected twice in the same controller, what will be true about the id values?Solution
Step 1: Understand transient scope behavior
Transient scope creates a new instance every time the service is injected, so each injection gets a fresh object.Step 2: Apply to the
Sinceidpropertyidis set to a random number on instance creation, each injection will have a differentid.Final Answer:
Each injection has a differentidvalue. -> Option DQuick Check:
Transient scope = new instance per injection [OK]
- Assuming transient shares instance like default
- Thinking id is constant or zero
- Expecting errors on multiple injections
Solution
Step 1: Understand request scope requirements
Request scope creates a new instance per HTTP request, so it only works if the provider is injected in a context handling requests.Step 2: Analyze the controller context
If the controller is not handling HTTP requests (e.g., injected in a service or singleton), the request scope behaves like singleton.Final Answer:
The controller injecting the service is not handling HTTP requests. -> Option AQuick Check:
Request scope needs HTTP request context [OK]
- Forgetting request scope needs HTTP context
- Confusing decorator absence with scope behavior
- Assuming string scope works like enum
Solution
Step 1: Understand request scope lifetime
Request-scoped services create one instance per HTTP request regardless of how many times they are injected.Step 2: Analyze injection chain
Even if injected into a transient service (new instance per injection), the request-scoped service instance remains the same within the same HTTP request.Final Answer:
One instance per HTTP request. -> Option AQuick Check:
Request scope = one instance per HTTP request [OK]
- Thinking transient causes multiple request-scoped instances
- Confusing transient and request scope lifetimes
- Assuming controller scope affects request-scoped service count
Ready to prove it?
Pass the test and this pattern moves unknown
