ConfigService.get('mode') after importing ConfigModule.forRoot({ mode: 'production' }) in the root module?import { Module, DynamicModule } from '@nestjs/common'; @Module({}) export class ConfigModule { static forRoot(options: { mode: string }): DynamicModule { return { module: ConfigModule, providers: [ { provide: 'CONFIG_OPTIONS', useValue: options, }, { provide: ConfigService, useFactory: (opts) => new ConfigService(opts.mode), inject: ['CONFIG_OPTIONS'], }, ], exports: [ConfigService], }; } } export class ConfigService { constructor(private mode: string) {} get(key: string) { if (key === 'mode') return this.mode; return null; } }
ConfigService is created using the useFactory and what value is passed from forRoot.The ConfigModule.forRoot method receives an options object with mode: 'production'. This value is provided as CONFIG_OPTIONS. The ConfigService is created using a factory that receives these options and sets the mode property. So, calling get('mode') returns 'production'.
forRootAsync in NestJS?useFactory with async and proper injection.Option D correctly uses useFactory with async and injects CONFIG_OPTIONS to create ConfigService. It awaits the promise returned by the factory. Other options either misuse useValue with a promise or have wrong injection.
import { Module, DynamicModule, Injectable, Inject } from '@nestjs/common'; @Module({}) export class ApiModule { static forRoot(apiKey: string): DynamicModule { return { module: ApiModule, providers: [ { provide: 'API_KEY', useValue: apiKey, }, ApiService, ], exports: [ApiService], }; } } @Injectable() export class ApiService { constructor(@Inject('API_KEY') private readonly key: string) {} }
In NestJS, to inject a custom provider like 'API_KEY' into a constructor parameter, you must use the @Inject('API_KEY') decorator. Without it, Nest cannot resolve the dependency and throws an error. The other options are incorrect because the provider exists and ApiService is injectable by default if decorated.
Dynamic modules allow passing configuration options when importing them, so the same module can behave differently depending on the parameters. This makes modules reusable and flexible. The other options describe unrelated features.
service.getValue() after this dynamic module setup?service.getValue() return after importing ExampleModule.forFeature({ value: 42 })?import { Module, DynamicModule, Injectable, Inject } from '@nestjs/common'; @Injectable() export class ExampleService { constructor(@Inject('FEATURE_OPTIONS') private options: { value: number }) {} getValue() { return this.options.value; } } @Module({}) export class ExampleModule { static forFeature(options: { value: number }): DynamicModule { return { module: ExampleModule, providers: [ { provide: 'FEATURE_OPTIONS', useValue: options, }, ExampleService, ], exports: [ExampleService], }; } }
The ExampleModule.forFeature method provides FEATURE_OPTIONS with { value: 42 }. The ExampleService injects this and returns options.value, which is 42.