Questions
1 of 25
1What are dynamic modules in NestJS and when do you use them?
2How would you lazy-load a module in NestJS to improve startup time?
3What are the four properties inside @Module() decorator?
4What is the difference between a shared module and a global module in NestJS?
5What is a NestJS module, and why is it needed?
6What is the difference between exports in a module and making a provider global with @Global()?
7What is the purpose of a module in NestJS and what does the @Module() decorator do?
8Are NestJS modules singletons? What does that mean practically?
9What is a feature module in NestJS and why should you use them instead of putting everything in AppModule?
10Can a feature module re-export an imported module in NestJS? When is this useful?
11What does the @Global() decorator do and how does it change module behaviour in NestJS?
12What are the risks of overusing @Global() modules in NestJS?
13How do you register a global module exactly once in NestJS and what happens if you register it twice?
14What is the naming convention for dynamic module static methods in NestJS and what does each name signal?
15How do you structure a feature module that has sub-features in NestJS?
16What is the difference between a shared module and a global module in NestJS?
17How can a dynamic module expose a forFeature() method that works alongside a forRoot() registration in NestJS?
18What is the module encapsulation rule in NestJS and why does it matter?
19What must the object returned by a dynamic module's static method always include in NestJS?
20What is the difference between imports and providers inside @Module()?
21What is a shared module in NestJS?
22When should you export a module vs export individual providers from a module in NestJS?
23What is a dynamic module in NestJS and how is it different from a static module?
24How do you implement forRootAsync() in a dynamic module so that options can be loaded asynchronously from ConfigService?
25How do you write a dynamic module that supports both sync (register) and async (registerAsync) configuration sharing the same internal providers?
01 / 25

What are dynamic modules in NestJS and when do you use them?

A dynamic module returns a DynamicModule object from a static method (conventionally forRoot(), forFeature(), or register()) so the caller can pass configuration at import time. Use it for library modules like config, TypeORM, or mailer where behavior must vary per consumer.

Dynamic modules allow you to create customizable modules whose providers are determined at import time, based on options passed by the consuming module.

Dynamic module example
Common naming conventions:
  1. 1

    forRoot() — used once at the app level with global configuration.

  2. 2

    forFeature() — used in feature modules with feature-specific config (e.g., TypeORM entities).

  3. 3

    register() — general-purpose configuration, used in any module.

Difficulty: 5/10
Topics: module configuration, conditional providers, library reuse

Scenario Questions

0-2 years experience
  1. 1

    We need to add a third‑party logging service that requires an API key at startup. How would you expose that service using a dynamic module?

  2. 2

    If you import a module that offers a forRoot method but you forget to call it, what will happen when you try to inject its service?

2-5 years experience
  1. 1

    You have a feature flag that should enable a payment provider only in production. Walk me through how you’d use a dynamic module to conditionally register that provider.

  2. 2

    During debugging you see an error that a provider from a dynamic module isn’t being injected. What are the common reasons for this failure?

5-8 years experience
  1. 1

    Design a multi‑tenant SaaS where each tenant gets its own database connection. Explain how you’d structure the connection module as a dynamic module and discuss any trade‑offs.

  2. 2

    If you end up loading dozens of dynamic modules at runtime, what performance or memory concerns arise and how would you mitigate them?

8+ years experience
  1. 1

    Your organization wants to share a common authentication library across several microservices owned by different teams. How would you package it as a dynamic module to support versioning, backward compatibility, and independent deployment?

  2. 2

    Consider a scenario where the same dynamic module needs to be used by both NestJS and a plain Node.js service. What architectural decisions would you make to keep the module reusable while preserving testability?

Follow-up Questions

  • What’s the practical difference between calling forRoot versus importing the module directly?
  • How does the scope of providers inside a dynamic module affect their lifecycle?
  • Can you test a dynamic module without providing real configuration values?