0
0
NestJSframework~5 mins

Global modules in NestJS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a global module in NestJS?
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()
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
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
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
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
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.