Consider a NestJS feature module that provides a service. What happens when this module is imported into the root module?
import { Module, Injectable } from '@nestjs/common'; @Injectable() export class GreetingService { getGreeting() { return 'Hello from Feature Module!'; } } @Module({ providers: [GreetingService], exports: [GreetingService], }) export class FeatureModule {} @Module({ imports: [FeatureModule], }) export class AppModule {} // In a controller inside AppModule constructor(private greetingService: GreetingService) {} getHello() { return this.greetingService.getGreeting(); }
Think about how providers are shared when exported from a module.
When a feature module exports a provider, importing that module makes the provider available for injection in the importing module.
Identify the correct NestJS feature module syntax that includes a controller and a service.
Check the property names and array syntax in the @Module decorator.
The @Module decorator expects arrays for controllers and providers properties. Property names must be plural and spelled correctly.
Given a feature module that provides a service but the service injection fails with 'Nest can't resolve dependencies' error, what is the likely cause?
import { Module, Injectable } from '@nestjs/common'; @Injectable() export class DataService { getData() { return 'data'; } } @Module({ providers: [DataService], // exports: [DataService], // This line is missing }) export class DataModule {} @Module({ imports: [DataModule], }) export class AppModule {} // In a controller inside AppModule constructor(private dataService: DataService) {}
Check if the service is exported from the module that provides it.
Providers must be exported from a feature module to be available for injection in modules that import it.
Consider two feature modules each providing a service with a counter property initialized to 0. If both modules are imported into AppModule and the services are injected separately, what are the counter values after incrementing each once?
import { Module, Injectable } from '@nestjs/common'; @Injectable() export class CounterService { counter = 0; increment() { this.counter++; } } @Module({ providers: [CounterService], exports: [CounterService], }) export class ModuleA {} @Module({ providers: [CounterService], exports: [CounterService], }) export class ModuleB {} @Module({ imports: [ModuleA, ModuleB], }) export class AppModule {} // In a controller constructor( private counterA: CounterService, private counterB: CounterService ) {} someMethod() { this.counterA.increment(); this.counterB.increment(); return { a: this.counterA.counter, b: this.counterB.counter }; }
Think about how NestJS creates service instances per module.
Each module provides its own instance of CounterService, so increments affect separate counters.
Choose the most accurate description of feature modules in NestJS.
Consider how NestJS encourages modular design.
Feature modules group related code to keep the app organized and allow selective sharing of providers via exports.