Consider a NestJS module decorated with @Global(). What is the effect of this decorator on the module's providers?
import { Module, Global } from '@nestjs/common'; @Global() @Module({ providers: [MyService], exports: [MyService], }) export class MyGlobalModule {}
Think about how @Global() affects module imports and provider availability.
When a module is decorated with @Global(), its exported providers become available automatically in every module without needing to import the global module explicitly.
Which of the following code snippets correctly defines a global module in NestJS?
Remember the order and syntax of decorators in TypeScript.
The @Global() decorator must be placed above the @Module() decorator to correctly mark the module as global.
Given a global module exporting a service, what happens when another module injects that service without importing the global module?
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(); } }
Think about how global modules affect provider injection.
Global modules make their exported providers available everywhere, so FeatureModule can inject GlobalService without importing GlobalModule.
Consider two global modules both exporting a provider with the same token. What error occurs and why?
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 {}
Think about how NestJS handles provider tokens in the global scope.
Having two global modules exporting providers with the same token causes a runtime error because NestJS cannot resolve which provider to use.
You want to create a global module that provides a service configurable with options passed during module import. Which approach correctly supports this pattern?
Think about how NestJS supports dynamic modules and global scope.
The recommended pattern is to create a global module with a static forRoot() method that returns a dynamic module configured with providers using the passed options.