NestJS NestJS 4 — Questions and Answers
Question 1: What is the purpose of the @Module() decorator's 'exports' array in NestJS?
- To expose the module to the internet
- To make providers available to other modules that import this module (Correct answer)
- To list all controllers of the module
- To define which providers are global
Correct answer: To make providers available to other modules that import this module
The exports array specifies which providers from the current module can be used by other modules that import it.
Question 2: How do you make a NestJS module available globally without importing it in every module?
- Add it to the AppModule's controllers array
- Use the @Global() decorator on the module (Correct answer)
- Use @Injectable({ scope: Scope.DEFAULT })
- Register it in the bootstrap function
Correct answer: Use the @Global() decorator on the module
The @Global() decorator marks a module as global-scope so its exported providers are available throughout the application without needing explicit imports.
Question 3: What is a Dynamic Module in NestJS?
- A module that loads lazily at runtime
- A module that returns a DynamicModule object from a static method, allowing configuration at import time (Correct answer)
- A module that generates controllers dynamically
- A module that uses async providers only
Correct answer: A module that returns a DynamicModule object from a static method, allowing configuration at import time
Dynamic modules expose a static method (like register() or forRoot()) that returns a DynamicModule object, enabling customizable module configuration when imported.
Question 4: Which NestJS feature allows deferring a provider's initialization until it's first requested?
- Lazy modules
- Circular dependency resolution
- Lazy-loaded providers with lazyInject()
- Asynchronous providers using useFactory with async/await (Correct answer)
Correct answer: Asynchronous providers using useFactory with async/await
Asynchronous providers using useFactory with async/await defer initialization until the factory resolves, useful for database connections and external service setup.
Question 5: In NestJS, what is the purpose of the forwardRef() utility?
- To lazily import external modules
- To resolve circular dependencies between modules or providers (Correct answer)
- To forward HTTP requests to another service
- To reference future route handlers
Correct answer: To resolve circular dependencies between modules or providers
forwardRef() is used to resolve circular dependencies by wrapping the class reference in a function, allowing NestJS to defer the reference resolution.
Question 6: Which scope makes a NestJS provider create a new instance for each incoming request?
- Scope.SINGLETON
- Scope.TRANSIENT
- Scope.REQUEST (Correct answer)
- Scope.PROTOTYPE
Correct answer: Scope.REQUEST
Scope.REQUEST creates a new instance of the provider for each incoming request, useful for request-scoped data like user context.
Question 7: What is the difference between Scope.TRANSIENT and Scope.REQUEST in NestJS?
- TRANSIENT creates a new instance per injection site; REQUEST creates one instance per HTTP request (Correct answer)
- TRANSIENT is for WebSocket only; REQUEST is for HTTP only
- They are identical in behavior
- TRANSIENT is global; REQUEST is module-scoped
Correct answer: TRANSIENT creates a new instance per injection site; REQUEST creates one instance per HTTP request
TRANSIENT providers get a new instance every time they are injected, while REQUEST providers share one instance across all injections within a single request lifecycle.
What is the purpose of the @Module() decorator's 'exports' array in NestJS?