Performance: Module decorator and metadata
This affects the initial application startup time and memory usage by controlling how modules and their metadata are loaded and instantiated.
@Module({ imports: [HeavyModule], controllers: [], providers: [] })
export class AppModule {}@Module({ imports: [HeavyModule, UnusedModule], controllers: [], providers: [] })
export class AppModule {}| Pattern | Module Imports | Startup Delay | Memory Usage | Verdict |
|---|---|---|---|---|
| Import all modules eagerly | Many heavy modules | High (100-200ms+) | High | [X] Bad |
| Import only needed modules | Minimal required modules | Low (50ms or less) | Low | [OK] Good |
@Module decorator in NestJS?@Module decorator organizes related parts of the app such as controllers and providers.@Module.AppController and a provider named AppService?@Module decorator expects arrays for controllers and providers properties.AppModule.imports.length?
@Module({
imports: [CommonModule, DatabaseModule],
controllers: [AppController],
providers: [AppService]
})
export class AppModule {}imports array contains two modules: CommonModule and DatabaseModule.AppModule.imports.length equals 2.@Module({
imports: CommonModule,
controllers: [AppController],
providers: [AppService]
})
export class AppModule {}imports property must be an array, but here it is a single module without brackets.controllers and providers are correctly arrays. The module class does not require a constructor by default.SharedService from SharedModule so other modules can use it. Which is the correct way to configure SharedModule to allow this?providers (to provide it) and exports (to make it available to importers). imports is for modules, not services. Missing either providers or exports prevents sharing.Pass the test and this pattern moves unknown