Questions
5 of 24
1How does NestJS resolve constructor injection at runtime?
2What does @Optional() decorator do in NestJS?
3What is the difference between moduleRef.get() and moduleRef.resolve() in NestJS?
4When do you need @Inject(TOKEN) vs plain constructor injection in NestJS?
5What is ModuleRef in NestJS and when would you use it?
6What are the three provider scopes in NestJS and when is each appropriate?
7What is the correct architectural solution for circular dependencies beyond forwardRef()?
8How do you detect and debug circular dependency errors in large NestJS projects?
9What is property-based injection in NestJS and when should you use it?
10How do you write a unit test for a NestJS service that has injected dependencies?
11What is a DI token and what types can be used as tokens in NestJS?
12How do you pass a custom REQUEST object to a REQUEST-scoped provider for queues or CRON jobs in NestJS?
13How does forwardRef() solve circular dependencies in NestJS and how does it work internally?
14How do you implement the Strategy pattern using NestJS DI?
15How do you replace a provider in a NestJS test module using overrideProvider()?
16What is Inversion of Control (IoC) and how does NestJS implement it?
17What is the difference between Dependency Injection (DI) and Inversion of Control (IoC)?
18What is scope propagation in NestJS and why can it impact performance?
19How do you inject the raw HTTP request object inside a REQUEST-scoped provider in NestJS?
20How does moduleRef.resolve() work with scoped providers and what is ContextIdFactory used for?
21What role does reflect-metadata play in NestJS DI?
22What is a circular dependency in NestJS and how does it manifest at runtime?
23What is LazyModuleLoader in NestJS and how does it differ from standard DI?
24What happens to REQUEST-scoped providers during WebSocket connections or microservice message handling in NestJS?
05 / 24

What is ModuleRef in NestJS and when would you use it?

ModuleRef is a reference to the current module's DI container. Inject it to resolve providers imperatively at runtime — useful when you need a provider conditionally or need to retrieve a transient/request-scoped provider dynamically. Use moduleRef.get() for singletons and moduleRef.resolve() for scoped providers.

Using ModuleRef to resolve providers dynamically
Common use cases:
  1. 1

    Strategy pattern — dynamically choose which provider to use at runtime.

  2. 2

    Lazy initialization — resolve a heavy provider only when first needed.

  3. 3

    Factory services — create instances of transient providers on demand.

  4. 4

    moduleRef.get() is synchronous and only works for singleton providers.

  5. 5

    moduleRef.resolve() is async and creates a new DI sub-context for scoped providers.

Difficulty: 5/10
Topics: dynamic provider resolution, runtime dependency injection, module ref usage

Scenario Questions

0-2 years experience
  1. 1

    You need to get an instance of a service inside a NestJS guard, but you can't inject it via the constructor. How would you use ModuleRef to retrieve it?

  2. 2

    If you call moduleRef.get(MyService, { strict: false }) and the provider isn't registered, what will happen at runtime?

  3. 3

    What would be the result of requesting a request‑scoped provider using ModuleRef from within a singleton?

2-5 years experience
  1. 1

    During a feature rollout you want to lazily load a heavy third‑party client only when a specific endpoint is hit. Walk me through how you'd use ModuleRef to achieve that and the trade‑offs you consider.

  2. 2

    Your app started throwing 'Nest can't resolve dependencies' after moving a provider into a dynamic module. How could ModuleRef help you debug or fix the issue?

  3. 3

    You have a plugin system where modules are registered at runtime. How would you use ModuleRef to instantiate a plugin's service and ensure its dependencies are satisfied?

5-8 years experience
  1. 1

    At high traffic you notice that using ModuleRef.get inside a controller adds latency. What strategies would you employ to mitigate the performance impact while keeping dynamic resolution?

  2. 2

    Design a mechanism for hot‑reloading feature modules without restarting the NestJS app. How would ModuleRef be involved, and what edge cases must you handle?

  3. 3

    Explain how you would coordinate ModuleRef usage across multiple microservices that share a common library, ensuring consistent provider instances.

8+ years experience
  1. 1

    Your organization is migrating a monolith to a modular NestJS architecture. How would you leverage ModuleRef to gradually refactor code, and what governance policies would you set to avoid runtime injection pitfalls?

  2. 2

    When several teams add dynamic modules that rely on the same token, what architectural guidelines would you establish around ModuleRef usage to prevent token collisions and maintain testability?

  3. 3

    Consider a long‑running background job system that resolves providers at runtime based on a job type stored in a database. How would you design this using ModuleRef to be resilient to schema changes and versioned modules?

Follow-up Questions

  • What are the risks of using ModuleRef inside a singleton service?
  • How does the 'strict' flag change the behavior of ModuleRef.get?
  • In what ways does ModuleRef improve or hurt testability compared to @Inject?