0
0
NestJSframework~10 mins

Global modules in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Global modules
Create Module
Add @Global Decorator
Import Module in Root Module
Module Becomes Global
Services Available Everywhere
No Need to Import Again
This flow shows how a module becomes global in NestJS by adding @Global and importing it once, making its services available everywhere without re-importing.
Execution Sample
NestJS
import { Module, Global } from '@nestjs/common';

@Global()
@Module({
  providers: [ConfigService],
  exports: [ConfigService],
})
export class ConfigModule {}
Defines a global ConfigModule that provides ConfigService to the whole app without needing to import it multiple times.
Execution Table
StepActionEffectModule StateService Availability
1Define ConfigModule with @GlobalMarks module as globalConfigModule(global=true)ConfigService not yet available
2Import ConfigModule in AppModuleRegisters module globallyConfigModule(global=true, imported in AppModule)ConfigService available globally
3Use ConfigService in Any ModuleNo need to import ConfigModule againAll modules can access ConfigServiceConfigService injected successfully
4Try to import ConfigModule again in another moduleIgnored due to global statusNo duplicate importsConfigService still available
5Application runsGlobal services accessible everywhereStable global module stateConfigService usable in all modules
💡 Global module imported once, services available app-wide without repeated imports
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
ConfigModule.globalfalsetruetruetruetrue
ConfigService.availablefalsefalsetruetruetrue
AppModule.imports[][][ConfigModule][ConfigModule][ConfigModule]
Key Moments - 2 Insights
Why don't we need to import a global module in every module that uses its services?
Because the @Global decorator marks the module as global, once imported in the root module, its exported providers are available everywhere automatically, as shown in execution_table step 3.
What happens if we try to import a global module multiple times?
NestJS ignores repeated imports of a global module to avoid duplication, so the module stays imported once, as shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does ConfigService become available globally?
AStep 4
BStep 1
CStep 2
DStep 5
💡 Hint
Check the 'Service Availability' column in execution_table row for Step 2
According to variable_tracker, what is the value of ConfigModule.global after Step 1?
Afalse
Btrue
Cundefined
Dnull
💡 Hint
Look at the 'ConfigModule.global' row and 'After Step 1' column in variable_tracker
If we remove the @Global decorator, how would the execution_table change?
AConfigService would not be available globally after Step 2
BConfigModule would still be global
CConfigService would be available everywhere without import
DNo change in service availability
💡 Hint
Refer to the concept_flow and execution_table steps about @Global effect
Concept Snapshot
Global modules in NestJS:
- Use @Global() decorator on a module
- Import it once in root module
- Its exported providers become available app-wide
- No need to import again in other modules
- Helps share common services easily
Full Transcript
In NestJS, a global module is created by adding the @Global decorator to a module class. This marks the module as global, so when it is imported once in the root module, its exported services become available throughout the entire application. This means other modules can use those services without importing the global module again. The execution table shows the steps: defining the module with @Global, importing it in the root module, and then using its services anywhere. Trying to import the global module multiple times does not cause duplication because NestJS ignores repeated imports of global modules. The variable tracker shows how the global flag and service availability change step by step. This approach simplifies sharing common services like configuration or logging across many modules without repetitive imports.