Questions
17 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?
17 / 25

How can a dynamic module expose a forFeature() method that works alongside a forRoot() registration in NestJS?

forRoot() registers global configuration once. forFeature() registers feature-specific providers that depend on the global config — similar to how TypeORM works with entities. forFeature() relies on providers from forRoot() being already registered globally, so it can inject them via the DI container.

forRoot() and forFeature() working together
forRoot vs forFeature responsibilities:
  1. 1

    forRoot — registers infrastructure once globally: connections, global config, shared clients.

  2. 2

    forFeature — registers per-module resources: entity repositories, feature-scoped services.

  3. 3

    forFeature providers can inject forRoot providers because global providers are available app-wide.

  4. 4

    This pattern is used by TypeOrmModule, MongooseModule, and SequelizeModule in the NestJS ecosystem.

Difficulty: 6/10
Topics: Dynamic modules, forRoot/forFeature pattern, module registration

Scenario Questions

0-2 years experience
  1. 1

    You need to add a new repository to an existing NestJS module that already uses forRoot(). How would you expose it using a forFeature() method?

  2. 2

    If you import a feature module that calls forFeature() twice with different providers, what will the injected service resolve to?

  3. 3

    What do you need to import in a feature module to get access to a service that was registered via the core module's forRoot()?

2-5 years experience
  1. 1

    Your team added a new feature module that calls forFeature(), but the injected service is undefined at runtime. Walk me through how you'd debug the issue.

  2. 2

    Explain the trade‑offs of placing a shared provider in forRoot() versus in each module's forFeature() when you have several feature modules.

  3. 3

    If a feature module also needs its own configuration in addition to the global config from forRoot(), how would you design the API?

5-8 years experience
  1. 1

    Design a dynamic module that supports global configuration via forRoot() and per‑feature overrides via forFeature(). What edge cases must you guard against?

  2. 2

    How would you ensure that providers registered through forFeature() are correctly scoped and don’t cause memory leaks in a high‑traffic NestJS service?

  3. 3

    Discuss the performance implications of loading many feature modules with forFeature() at startup versus lazy‑loading them.

8+ years experience
  1. 1

    Your organization is migrating a monolithic NestJS app to microservices. How would you refactor existing forRoot/forFeature modules to support independent deployment while preserving shared configuration?

  2. 2

    What versioning or governance strategy would you put in place for a shared library that exposes both forRoot() and forFeature() to avoid breaking changes across multiple teams?

  3. 3

    Multiple teams need to extend a core module with custom providers via forFeature(). How would you design the module’s API and documentation to ensure consistency and prevent circular dependencies?

Follow-up Questions

  • What would happen if you forgot to export the providers from the forFeature() call?
  • How does NestJS determine the scope of a provider registered via forFeature() versus forRoot()?
  • Can you describe a situation where two feature modules might conflict because of shared provider tokens?