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

What is the difference between imports and providers inside @Module()?

providers declares classes that belong to this module's DI scope — NestJS instantiates them. imports lists other modules whose exported providers this module wants to use. A common mistake is adding a service class directly to imports instead of its module, which causes a cryptic startup error.

The distinction is fundamental: providers owns the classes this module is responsible for creating. imports borrows functionality from other modules by referencing the module class, never the service class directly.

Correct use of imports vs providers
Rules to remember:
  1. 1

    providers — classes this module owns and instantiates.

  2. 2

    imports — module classes (not service classes) that this module depends on.

  3. 3

    If a service lives in another module, import that module and ensure it exports the service.

  4. 4

    Adding a service directly to imports is the most common NestJS beginner mistake.

Difficulty: 5/10
Topics: module imports, providers, dependency injection

Scenario Questions

0-2 years experience
  1. 1

    You need to add a LoggingService to a feature module. Should it go in the imports array or the providers array of @Module(), and why?

  2. 2

    If you forget to list a service in the providers array but try to inject it in a controller, what runtime error will you see?

  3. 3

    When you import a SharedModule that already exports a ConfigService, do you also need to list ConfigService in the providers of the consuming module?

2-5 years experience
  1. 1

    You added AuthModule to the imports of UsersModule, but AuthService injection fails. Walk me through how you'd debug the problem.

  2. 2

    Explain the trade‑offs between re‑exporting a provider from a shared module versus declaring it in each feature module’s providers.

  3. 3

    A circular dependency appeared after moving a UtilityService from CoreModule to a feature module's providers. How does the imports vs providers distinction affect this issue?

5-8 years experience
  1. 1

    Design a strategy for organizing providers and imports across dozens of microservice modules to minimize duplicate instances and startup time.

  2. 2

    How would you refactor a legacy monolith where many modules import each other just to access a few services, considering the imports vs providers distinction?

  3. 3

    Discuss the impact on memory usage and request‑scoped providers when a module is imported multiple times versus providing the service locally.

8+ years experience
  1. 1

    At a company‑wide level you need a new cross‑cutting FeatureFlagService available to all teams without tight coupling. How would you use imports, exports, and dynamic modules to achieve this while keeping the architecture clean?

  2. 2

    When migrating from a global shared module pattern to a more granular module system, what risks arise around provider scope and import hierarchy, and how would you mitigate them?

Follow-up Questions

  • When would you choose to export a provider from a module instead of listing it everywhere?
  • What happens if the same class appears in both the imports and providers arrays of a module?
  • How does NestJS decide which instance of a provider to inject when multiple modules import the same module?