A global module is a module that is automatically available across the entire application without needing to import it in other modules.
Click to reveal answer
beginner
How do you make a module global in NestJS?
You add the @Global() decorator above the module class and then export the providers you want to share.
Click to reveal answer
intermediate
Why use global modules in NestJS?
Global modules help avoid repetitive imports of common services or providers in many modules, making code cleaner and easier to maintain.
Click to reveal answer
intermediate
Can you import a global module multiple times in NestJS?
No, once a module is marked global, you do not need to import it in other modules. Importing it again has no effect.
Click to reveal answer
beginner
Show a simple example of a global module in NestJS.
```typescript
import { Module, Global } from '@nestjs/common';
@Global()
@Module({
providers: [CommonService],
exports: [CommonService],
})
export class CommonModule {}
```
Click to reveal answer
What decorator makes a module global in NestJS?
A@Module()
B@Global()
C@Injectable()
D@Controller()
✗ Incorrect
The @Global() decorator marks a module as global so it can be used without importing it everywhere.
If a module is global, do you need to import it in every other module?
AYes, always
BOnly in controllers
CNo, it is available automatically
DOnly if it has providers
✗ Incorrect
Global modules are automatically available in all modules without importing.
What must you do to share a service from a global module?
AExport the service in the module exports array
BImport the service in every module
CUse @Injectable() only
DNothing, it is shared by default
✗ Incorrect
You must export the service in the module's exports array to share it.
Which of these is NOT a benefit of global modules?
ACleaner module code
BMake services available app-wide
CAvoid repetitive imports
DAutomatically reload modules on change
✗ Incorrect
Global modules do not affect module reloading behavior.
Where do you place the @Global() decorator?
AAbove the module class
BAbove the service class
CInside the module imports array
DInside the main.ts file
✗ Incorrect
The @Global() decorator is placed above the module class declaration.
Explain how to create and use a global module in NestJS.
Think about how to share services app-wide without importing everywhere.
You got /4 concepts.
What are the advantages and possible downsides of using global modules in NestJS?
Consider both code simplicity and clarity.
You got /2 concepts.
Practice
(1/5)
1. What is the main purpose of marking a module with @Global() in NestJS?
easy
A. To disable dependency injection for the module
B. To make its providers available across the entire application without importing the module repeatedly
C. To automatically register the module as a controller
D. To restrict the module's providers to only one feature module
Solution
Step 1: Understand the role of @Global()
The @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 B
Quick Check:
Global modules share providers app-wide [OK]
Hint: Global modules share providers app-wide without imports [OK]
Common Mistakes:
Thinking @Global restricts providers to one module
Confusing @Global with controller registration
Assuming @Global disables dependency injection
2. Which of the following is the correct way to declare a global module in NestJS?
easy
A. @Global() @Module({ providers: [MyService], exports: [MyService] }) export class MyModule {}
B. @Module({ global: true, providers: [MyService], exports: [MyService] }) export class MyModule {}
C. @GlobalModule() @Module({ providers: [MyService] }) export class MyModule {}
D. @Module({ providers: [MyService], global: true }) export class MyModule {}
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 A
Quick Check:
Use @Global() decorator above @Module() [OK]
Hint: Use @Global() decorator above @Module() to declare global module [OK]
Common Mistakes:
Using 'global: true' inside @Module() which is invalid
But when you try to inject LoggerService in another module, you get an error: Nest can't resolve dependencies. What is the likely cause?
medium
A. You forgot to import LoggerModule in the root module
B. You did not mark LoggerModule with @Global()
C. You forgot to export LoggerService from LoggerModule
D. You did not add @Injectable() to LoggerService
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 D
Quick Check:
Missing @Injectable() on service causes injection errors [OK]
Hint: Check if service has @Injectable() decorator [OK]
Common Mistakes:
Assuming global modules don't need imports anywhere
Forgetting to export providers
Confusing missing @Global() with injection errors
5. You want to create a global module that provides a configuration service with environment variables. Which of the following is the best practice to ensure the service is singleton and accessible everywhere without repeated imports?
hard
A. Mark the module with @Global(), export the service, and import it only once in the root module
B. Mark the module with @Global(), export the service, and import the module in every feature module
C. Mark the module with @Global(), export the service, and do not import it anywhere else
D. Do not mark the module global; import it in every module that needs it
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 A
Quick Check:
Import global modules once in root module [OK]
Hint: Global modules: mark @Global(), export, import once in root [OK]
Common Mistakes:
Importing global modules in every feature module
Not importing global modules at all
Not marking the module as global
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.