The default provider scope is Singleton. This means one instance of the provider is created and shared across the entire application.
Click to reveal answer
intermediate
Explain the Request provider scope in NestJS.
The Request scope creates a new instance of the provider for each incoming HTTP request. This helps isolate data per request.
Click to reveal answer
intermediate
What does the Transient provider scope do in NestJS?
The Transient scope creates a new instance of the provider every time it is injected, regardless of the request or module.
Click to reveal answer
advanced
How does the Request scope differ from the Transient scope?
The Request scope creates one instance per HTTP request, shared within that request. The Transient scope creates a new instance every time it is injected, even multiple times in the same request.
Click to reveal answer
intermediate
Why would you use Transient scope for a provider?
Use Transient when you want a fresh instance every time to avoid shared state or side effects, like for stateless helpers or utilities.
Click to reveal answer
What scope does NestJS use by default for providers?
ASession
BSingleton
CTransient
DRequest
✗ Incorrect
By default, NestJS providers are singletons, meaning one instance is shared across the app.
Which provider scope creates a new instance for each HTTP request?
ASingleton
BTransient
CGlobal
DRequest
✗ Incorrect
The Request scope creates a new provider instance per HTTP request.
If you want a new provider instance every time it is injected, which scope should you use?
ASingleton
BRequest
CTransient
DScoped
✗ Incorrect
Transient scope creates a new instance every time the provider is injected.
Which scope shares the same instance within a single HTTP request but creates new instances for different requests?
ARequest
BSingleton
CTransient
DGlobal
✗ Incorrect
Request scope shares one instance per HTTP request.
Why might you avoid using Singleton scope for stateful providers?
ABecause Singleton shares the same instance across all requests, causing shared state issues
BBecause Singleton creates multiple instances
CBecause Singleton creates new instances per injection
DBecause Singleton is slower
✗ Incorrect
Singleton scope shares one instance app-wide, which can cause unwanted shared state for stateful providers.
Describe the differences between default, request, and transient provider scopes in NestJS.
Think about how many instances are created and when.
You got /3 concepts.
When would you choose to use the transient scope over the request or default scopes in NestJS?
Consider cases where sharing instances could cause problems.
You got /3 concepts.
Practice
(1/5)
1. In NestJS, what does the default provider scope mean for a service instance?
easy
A. A new instance is created for each HTTP request.
B. A single instance is shared across the entire application.
C. A new instance is created every time the service is injected.
D. The service instance is destroyed after each method call.
@Injectable({ scope: Scope.TRANSIENT }) -> Option C
Quick Check:
Use Scope.TRANSIENT enum for transient scope [OK]
Hint: Use Scope.TRANSIENT enum inside @Injectable decorator [OK]
Common Mistakes:
Using string 'transient' instead of Scope enum
Confusing transient with request scope
Using invalid decorator properties
3. Consider this NestJS service with transient scope:
@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?
medium
A. Both injections share the same id value.
B. The service will throw an error on multiple injections.
C. The id value is always zero.
D. Each injection has a different id value.
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 id property
Since id is set to a random number on instance creation, each injection will have a different id.
Final Answer:
Each injection has a different id value. -> Option D
Quick Check:
Transient scope = new instance per injection [OK]
Hint: Transient scope means new instance each injection [OK]
Common Mistakes:
Assuming transient shares instance like default
Thinking id is constant or zero
Expecting errors on multiple injections
4. You set a service to request scope but notice it behaves like a singleton. What is the most likely cause?
medium
A. The controller injecting the service is not handling HTTP requests.
B. The service was not decorated with @Injectable.
C. The service scope was set incorrectly using a string instead of Scope.REQUEST.
D. The service is injected only once in the entire app.
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 A
Quick Check:
Request scope needs HTTP request context [OK]
Hint: Request scope needs HTTP request context to work [OK]
Common Mistakes:
Forgetting request scope needs HTTP context
Confusing decorator absence with scope behavior
Assuming string scope works like enum
5. You have a service with request scope injected into a transient-scoped service, which is then injected into a controller. How many instances of the request-scoped service are created per HTTP request?
hard
A. One instance per HTTP request.
B. One instance per injection of the transient service.
C. One instance per controller instance.
D. Multiple instances per HTTP request depending on injections.
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 A
Quick Check:
Request scope = one instance per HTTP request [OK]
Hint: Request scope always one instance per HTTP request [OK]