Global modules let you share services or providers across your whole NestJS app without importing them everywhere.
Global modules in NestJS
import { Module, Global } from '@nestjs/common'; @Global() @Module({ providers: [MyService], exports: [MyService], }) export class MyGlobalModule {}
The @Global() decorator marks the module as global.
You must export the providers you want to share globally.
import { Module, Global } from '@nestjs/common'; @Global() @Module({ providers: [ConfigService], exports: [ConfigService], }) export class ConfigModule {}
import { Module, Global } from '@nestjs/common'; @Global() @Module({ providers: [LoggerService], exports: [LoggerService], }) export class LoggerModule {}
This example shows a global GreetingModule that provides GreetingService everywhere. The WelcomeService uses GreetingService without importing GreetingModule again. The console prints a greeting message.
import { Module, Global, Injectable } from '@nestjs/common'; @Injectable() export class GreetingService { sayHello(name: string): string { return `Hello, ${name}!`; } } @Global() @Module({ providers: [GreetingService], exports: [GreetingService], }) export class GreetingModule {} @Module({ imports: [GreetingModule], }) export class AppModule {} // Usage in any other module without importing GreetingModule again import { Injectable } from '@nestjs/common'; @Injectable() export class WelcomeService { constructor(private greetingService: GreetingService) {} welcomeUser(user: string): string { return this.greetingService.sayHello(user); } } // Simulate usage const welcomeService = new WelcomeService(new GreetingService()); console.log(welcomeService.welcomeUser('Alice'));
Global modules are registered once and shared app-wide.
Use global modules sparingly to avoid hidden dependencies.
You still need to import the global module once in your root module.
Global modules share providers across the whole app without repeated imports.
Mark a module with @Global() and export its providers.
Use global modules for common services like config or logging.
Practice
@Global() in NestJS?Solution
Step 1: Understand the role of
The@Global()@Global()decorator marks a module so its providers can be used app-wide without importing it in every module.Step 2: Compare options with this behavior
Only To make its providers available across the entire application without importing the module repeatedly correctly describes this behavior; others describe unrelated or incorrect effects.Final Answer:
To make its providers available across the entire application without importing the module repeatedly -> Option BQuick Check:
Global modules share providers app-wide [OK]
- Thinking @Global restricts providers to one module
- Confusing @Global with controller registration
- Assuming @Global disables dependency injection
Solution
Step 1: Recall the correct decorator for global modules
In NestJS,@Global()is used above the module class to mark it global.Step 2: Check syntax correctness
@Global() @Module({ providers: [MyService], exports: [MyService] }) export class MyModule {} correctly uses@Global()and@Module()with providers and exports arrays.Final Answer:
@Global() @Module({ providers: [MyService], exports: [MyService] }) export class MyModule {} -> Option AQuick Check:
Use @Global() decorator above @Module() [OK]
- Using 'global: true' inside @Module() which is invalid
- Using non-existent @GlobalModule() decorator
- Forgetting to export providers in global module
@Global()
@Module({
providers: [ConfigService],
exports: [ConfigService],
})
export class ConfigModule {}What happens if another module imports
ConfigModule again?Solution
Step 1: Understand global module behavior on imports
Global modules share their providers app-wide, so importing them multiple times does not duplicate providers.Step 2: Analyze the effect of importing a global module again
Importing a global module again is harmless; providers remain shared and singleton.Final Answer:
The providers are shared globally, so importing again has no effect -> Option CQuick Check:
Global modules share providers; repeated imports don't duplicate [OK]
- Assuming repeated imports cause errors
- Thinking providers become private on re-import
- Confusing global modules with normal modules
@Global()
@Module({
providers: [LoggerService],
exports: [LoggerService],
})
export class LoggerModule {}But when you try to inject
LoggerService in another module, you get an error: Nest can't resolve dependencies. What is the likely cause?Solution
Step 1: Check the module declaration
The module is marked global and exports LoggerService, so imports and exports are correct.Step 2: Verify LoggerService setup
If LoggerService lacks@Injectable(), NestJS cannot inject it, causing the error.Final Answer:
You did not add@Injectable()to LoggerService -> Option DQuick Check:
Missing @Injectable() on service causes injection errors [OK]
- Assuming global modules don't need imports anywhere
- Forgetting to export providers
- Confusing missing @Global() with injection errors
Solution
Step 1: Understand global module usage
Global modules provide their exported providers app-wide without needing to import them repeatedly.Step 2: Determine import requirements for global modules
Global modules must be imported once, typically in the root module; after that, providers are available app-wide without further imports.Final Answer:
Mark the module with@Global(), export the service, and import it only once in the root module -> Option AQuick Check:
Import global modules once in root module [OK]
- Importing global modules in every feature module
- Not importing global modules at all
- Not marking the module as global
Ready to prove it?
Pass the test and this pattern moves unknown
