Bird
Raised Fist0
NestJSframework~10 mins

Module re-exporting in NestJS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Module re-exporting
Create Module A
↓
Export Providers/Components
↓
Create Module B
↓
Import Module A in Module B
↓
Re-export Module A from Module B
↓
Other Modules Import Module B
↓
Access Providers from Module A via Module B
This flow shows how one module exports its providers, another module imports and re-exports it, so other modules can access the original providers through the re-exporting module.
Execution Sample
NestJS
import { Injectable, Module } from '@nestjs/common';

@Injectable()
class ServiceA {}

@Module({
  providers: [ServiceA],
  exports: [ServiceA],
})
export class ModuleA {}

@Module({
  imports: [ModuleA],
  exports: [ModuleA],
})
export class ModuleB {}
ModuleA exports ServiceA. ModuleB imports and re-exports ModuleA, so other modules importing ModuleB get ServiceA.
Execution Table
StepActionModule StateExports AvailableEffect
1Define ModuleA with ServiceA provider and export ServiceAModuleA createdServiceAServiceA is available to modules importing ModuleA
2Define ModuleB importing ModuleA and re-exporting ModuleAModuleB createdModuleAModules importing ModuleB get ServiceA indirectly
3Another module imports ModuleBOtherModule createdServiceA (via ModuleB)OtherModule can inject ServiceA without importing ModuleA directly
4Use ServiceA in OtherModuleServiceA injectedServiceAServiceA functionality accessible in OtherModule
5EndNo further changesNo changeExecution stops
💡 All modules defined and re-exporting completed, no further steps
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
ModuleA.exports[][ServiceA][ServiceA][ServiceA][ServiceA]
ModuleB.imports[][][ModuleA][ModuleA][ModuleA]
ModuleB.exports[][][ModuleA][ModuleA][ModuleA]
OtherModule.imports[][][][ModuleB][ModuleB]
OtherModule.availableProviders[][][][ServiceA][ServiceA]
Key Moments - 2 Insights
Why does OtherModule get access to ServiceA even though it only imports ModuleB?
Because ModuleB re-exports ModuleA, which exports ServiceA. So importing ModuleB also imports ModuleA's exports indirectly, as shown in execution_table step 3.
What happens if ModuleB imports ModuleA but does not re-export it?
OtherModule importing ModuleB would NOT get ServiceA automatically. It must import ModuleA directly to access ServiceA. This is because re-exporting is needed to pass exports along, as seen in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What does ModuleB export?
AServiceA directly
BModuleA
CNothing
DServiceA and ModuleA
💡 Hint
Check the 'Exports Available' column at step 2 in execution_table
At which step does OtherModule gain access to ServiceA?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Module State' and 'Exports Available' columns for OtherModule in execution_table
If ModuleB did not re-export ModuleA, what would OtherModule's available providers be after step 3?
A[ServiceA]
B[ModuleA]
C[]
D[ModuleB]
💡 Hint
Refer to key_moments about re-exporting necessity and execution_table step 2
Concept Snapshot
Module re-exporting in NestJS:
- ModuleA exports providers (e.g., ServiceA)
- ModuleB imports ModuleA and re-exports it
- Other modules importing ModuleB get access to ModuleA's exports
- Re-exporting passes exports through modules
- Without re-export, imports don't pass exports along
Full Transcript
Module re-exporting in NestJS means one module (ModuleB) imports another module (ModuleA) and then re-exports it. This allows other modules to import ModuleB and still get access to the providers exported by ModuleA. The flow starts by defining ModuleA with providers and exports. Then ModuleB imports and re-exports ModuleA. When another module imports ModuleB, it can use the providers from ModuleA without importing ModuleA directly. This is useful to organize and simplify module dependencies. The key is that re-exporting passes exports along the chain. Without re-exporting, the exports are not available to modules importing only the intermediate module.

Practice

(1/5)
1. What is the main purpose of module re-exporting in NestJS?
easy
A. To share modules through another module for better organization
B. To create multiple instances of the same module
C. To prevent modules from being imported anywhere
D. To automatically generate services inside a module

Solution

  1. Step 1: Understand module re-exporting concept

    Module re-exporting allows one module to share its imported modules with other modules by re-exporting them.
  2. Step 2: Identify the purpose in NestJS

    This helps keep the app organized and makes modules easier to maintain by avoiding repeated imports.
  3. Final Answer:

    To share modules through another module for better organization -> Option A
  4. Quick Check:

    Module re-exporting = sharing modules [OK]
Hint: Re-exporting means sharing modules via another module [OK]
Common Mistakes:
  • Thinking re-exporting creates new module instances
  • Believing it blocks module imports
  • Confusing it with automatic service generation
2. Which syntax correctly re-exports a module named SharedModule inside another module's decorator?
easy
A. @Module({ exports: [SharedModule] })
B. @Module({ imports: [], exports: [SharedModule] })
C. @Module({ imports: [SharedModule] })
D. @Module({ imports: [SharedModule], exports: [SharedModule] })

Solution

  1. Step 1: Recall NestJS module decorator structure

    To re-export a module, it must be both imported and exported in the module decorator.
  2. Step 2: Check each option

    The syntax with both imports: [SharedModule] and exports: [SharedModule] is correct. Syntaxes missing either array are incorrect.
  3. Final Answer:

    @Module({ imports: [SharedModule], exports: [SharedModule] }) -> Option D
  4. Quick Check:

    Import and export to re-export [OK]
Hint: Re-export needs both imports and exports arrays [OK]
Common Mistakes:
  • Only exporting without importing
  • Only importing without exporting
  • Leaving either array empty when re-exporting
3. Given these modules, what will be the output of console.log(appModuleImports) if AppModule imports CoreModule which re-exports SharedModule?
@Module({ imports: [SharedModule], exports: [SharedModule] })
export class CoreModule {}

@Module({ imports: [CoreModule] })
export class AppModule {}
Assuming appModuleImports is the list of modules available inside AppModule.
medium
A. [SharedModule]
B. [CoreModule, SharedModule]
C. [CoreModule]
D. []

Solution

  1. Step 1: Understand re-export effect on imports

    CoreModule imports and exports SharedModule, so any module importing CoreModule also gets SharedModule.
  2. Step 2: Analyze AppModule imports

    AppModule imports CoreModule, so it has access to both CoreModule and SharedModule.
  3. Final Answer:

    [CoreModule, SharedModule] -> Option B
  4. Quick Check:

    Re-exported modules appear in importing module imports [OK]
Hint: Re-exported modules appear as if directly imported [OK]
Common Mistakes:
  • Assuming only CoreModule is available
  • Thinking SharedModule is hidden
  • Ignoring exports array effect
4. Identify the error in this module re-exporting code:
@Module({
  imports: [SharedModule],
  exports: []
})
export class CoreModule {}
medium
A. SharedModule is imported but not exported, so it is not re-exported
B. SharedModule should not be in imports when re-exporting
C. exports array cannot be empty in any module
D. CoreModule must import AppModule to re-export SharedModule

Solution

  1. Step 1: Check imports and exports arrays

    SharedModule is imported but not exported, so CoreModule does not re-export it.
  2. Step 2: Understand re-export requirement

    To re-export a module, it must be listed in both imports and exports arrays.
  3. Final Answer:

    SharedModule is imported but not exported, so it is not re-exported -> Option A
  4. Quick Check:

    Re-export needs both imports and exports [OK]
Hint: Exports must include imported modules to re-export [OK]
Common Mistakes:
  • Leaving exports empty when re-exporting
  • Confusing imports-only as re-export
  • Thinking exports array is optional
5. You have three modules: SharedModule, CoreModule, and FeatureModule. CoreModule imports and re-exports SharedModule. FeatureModule imports CoreModule. How can you modify FeatureModule to also re-export SharedModule without importing it directly?
hard
A. Add imports: [SharedModule] in FeatureModule
B. Add exports: [SharedModule] in FeatureModule
C. Add exports: [CoreModule] in @Module decorator of FeatureModule
D. Add providers: [SharedModule] in FeatureModule

Solution

  1. Step 1: Understand re-export chaining

    FeatureModule imports CoreModule, which re-exports SharedModule, so FeatureModule has access to SharedModule.
  2. Step 2: Re-export SharedModule via CoreModule

    To re-export SharedModule from FeatureModule without importing it directly, FeatureModule must export CoreModule.
  3. Final Answer:

    Add exports: [CoreModule] in @Module decorator of FeatureModule -> Option C
  4. Quick Check:

    Export imported module to re-export its exports [OK]
Hint: Export imported module to re-export its exports [OK]
Common Mistakes:
  • Trying to export SharedModule without importing it
  • Adding SharedModule to providers array
  • Importing SharedModule directly instead of re-exporting
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard