0
0
NestJSframework~10 mins

Shared modules in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Shared modules
Create Shared Module
Declare Providers
Export Providers
Import Shared Module in Feature Modules
Use Shared Providers
Single Instance Across App
This flow shows how a shared module is created with providers, exports them, and then feature modules import it to use the same instances.
Execution Sample
NestJS
import { Module } from '@nestjs/common';
import { LoggerService } from './logger.service';

@Module({
  providers: [LoggerService],
  exports: [LoggerService],
})
export class SharedModule {}
Defines a shared module that provides and exports LoggerService for reuse.
Execution Table
StepActionModule StateProviders AvailableExports AvailableEffect
1Define SharedModule with LoggerService providerSharedModule createdLoggerServiceNone yetModule ready with provider
2Export LoggerService from SharedModuleSharedModule updatedLoggerServiceLoggerServiceLoggerService can be used outside
3Import SharedModule in FeatureModuleFeatureModule createdLoggerService (from SharedModule)NoneFeatureModule can access exported providers
4FeatureModule uses LoggerServiceFeatureModule runningLoggerService (from SharedModule)NoneSingle LoggerService instance shared
5Another FeatureModule imports SharedModuleAnother FeatureModule createdLoggerService (from SharedModule)NoneAlso shares LoggerService instance
6App runs with shared LoggerServiceApp runningLoggerService sharedLoggerServiceSingle instance used app-wide
💡 All feature modules share the same exported providers from SharedModule, ensuring single instances.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
SharedModule.providers[][LoggerService][LoggerService][LoggerService][LoggerService][LoggerService]
SharedModule.exports[][][LoggerService][LoggerService][LoggerService][LoggerService]
FeatureModule.providers[][][][][][]
FeatureModule.imports[][][SharedModule][SharedModule][SharedModule][SharedModule]
Key Moments - 3 Insights
Why do we export providers in the shared module?
Exporting providers (see step 2 in execution_table) makes them available to other modules that import the shared module. Without export, other modules cannot use those providers.
Does importing the shared module create new instances of providers each time?
No. As shown in steps 4 and 5, importing the shared module shares the same instance of providers across modules, avoiding duplication.
Can feature modules use providers from the shared module without importing it?
No. Feature modules must import the shared module (step 3) to access its exported providers.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is LoggerService made available to other modules?
AStep 1
BStep 2
CStep 4
DStep 3
💡 Hint
Check the 'Exports Available' column to see when LoggerService is exported.
According to variable_tracker, what is the state of FeatureModule.providers after step 4?
A[SharedModule]
B[LoggerService]
C[]
Dundefined
💡 Hint
Look at the FeatureModule.providers row under 'After Step 4' column.
If SharedModule did not export LoggerService, what would happen when FeatureModule tries to use it?
AFeatureModule would not have access to LoggerService
BLoggerService would be shared anyway
CFeatureModule would get its own LoggerService instance
DApp would crash immediately
💡 Hint
Refer to key_moments about exporting providers and step 2 in execution_table.
Concept Snapshot
Shared modules in NestJS:
- Create a module with providers
- Export providers to share them
- Import shared module in feature modules
- Providers are singletons shared app-wide
- Without export, providers are private
- Enables clean, reusable code
Full Transcript
In NestJS, a shared module is a module that provides services or providers that multiple other modules can use. First, you create the shared module and declare providers inside it. Then, you export those providers so other modules can access them. Feature modules import the shared module to use the exported providers. This sharing ensures a single instance of each provider is used across the app, avoiding duplication. If providers are not exported, other modules cannot use them. This pattern helps keep code clean and reusable by centralizing common services.