0
0
NestJSframework~20 mins

Global modules in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Global Module Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a module is marked as global in NestJS?

Consider a NestJS module decorated with @Global(). What is the effect of this decorator on the module's providers?

NestJS
import { Module, Global } from '@nestjs/common';

@Global()
@Module({
  providers: [MyService],
  exports: [MyService],
})
export class MyGlobalModule {}
AThe providers are automatically available in all modules without needing to import this module.
BThe providers are available only within this module and must be imported explicitly elsewhere.
CThe providers are available globally but only if the module is imported in the root module.
DThe providers are available globally but only if the module is imported in every module explicitly.
Attempts:
2 left
💡 Hint

Think about how @Global() affects module imports and provider availability.

📝 Syntax
intermediate
2:00remaining
Identify the correct syntax to create a global module in NestJS

Which of the following code snippets correctly defines a global module in NestJS?

A
import { Module, Global } from '@nestjs/common';

@Global()
@Module({
  providers: [Service],
  exports: [Service],
})
export class GlobalModule {}
B
import { Global } from '@nestjs/common';

@Global
@Module({
  providers: [Service],
  exports: [Service],
})
export class GlobalModule {}
C
import { Module } from '@nestjs/common';

@Module({
  providers: [Service],
  exports: [Service],
})
export class GlobalModule {}
D
import { Module, Global } from '@nestjs/common';

@Module({
  providers: [Service],
  exports: [Service],
})
@Global()
export class GlobalModule {}
Attempts:
2 left
💡 Hint

Remember the order and syntax of decorators in TypeScript.

state_output
advanced
2:00remaining
What is the output when accessing a global provider without importing its module?

Given a global module exporting a service, what happens when another module injects that service without importing the global module?

NestJS
import { Injectable, Global, Module } from '@nestjs/common';

@Injectable()
export class GlobalService {
  getMessage() {
    return 'Hello from global service';
  }
}

@Global()
@Module({
  providers: [GlobalService],
  exports: [GlobalService],
})
export class GlobalModule {}

@Module({})
export class FeatureModule {
  constructor(private readonly globalService: GlobalService) {}
  printMessage() {
    return this.globalService.getMessage();
  }
}
AA runtime error occurs: Nest can't resolve dependencies of FeatureModule.
B'Hello from global service' is returned successfully.
CThe service is undefined and causes a TypeError when calling getMessage.
DThe service returns null because it was not imported explicitly.
Attempts:
2 left
💡 Hint

Think about how global modules affect provider injection.

🔧 Debug
advanced
2:00remaining
Why does a global provider cause a duplicate provider error?

Consider two global modules both exporting a provider with the same token. What error occurs and why?

NestJS
import { Global, Module, Injectable } from '@nestjs/common';

@Injectable()
export class SharedService {}

@Global()
@Module({
  providers: [SharedService],
  exports: [SharedService],
})
export class GlobalModuleA {}

@Global()
@Module({
  providers: [SharedService],
  exports: [SharedService],
})
export class GlobalModuleB {}
ANo error occurs; NestJS merges providers automatically.
BA compile-time error occurs: Cannot redeclare class SharedService.
CThe last imported global module overrides the provider silently.
DA runtime error occurs: Duplicate provider token detected.
Attempts:
2 left
💡 Hint

Think about how NestJS handles provider tokens in the global scope.

🧠 Conceptual
expert
3:00remaining
How to properly share a global service with dynamic configuration?

You want to create a global module that provides a service configurable with options passed during module import. Which approach correctly supports this pattern?

AMark the service as <code>@Injectable({ global: true })</code> and pass options via dependency injection.
BCreate a global module without <code>forRoot()</code> and set configuration inside the service constructor using environment variables.
CUse <code>@Global()</code> on a module with a static <code>forRoot()</code> method returning a dynamic module with providers configured by options.
DUse <code>@Global()</code> on a module and pass options directly to the constructor of the service.
Attempts:
2 left
💡 Hint

Think about how NestJS supports dynamic modules and global scope.