Kind: Class
Source: packages/core/scanner.ts
Part of: Core
DependenciesScanner discovers an application’s module tree and reflects Nest metadata for imports, providers, controllers, exports, and dynamic module configuration. It populates the NestContainer during application initialization so the dependency injection system can resolve modules and their registered components.
Methods
| Method | Signature | Returns |
|---|---|---|
scan | scan(module: Type<any>, options: { overrides?: ModuleOverride[] }) | void |
scanForModules | `scanForModules({ |
moduleDefinition, lazy, scope = [], ctxRegistry = [], overrides = [],
}: ModulesScanParameters)|Promise<Module[]>| |insertModule|insertModule(moduleDefinition: any, scope: Type|Promise< | { moduleRef: Module; inserted: boolean; } | undefined >| |scanModulesForDependencies|scanModulesForDependencies(modules: Map<string, Module>)|void| |reflectImports|reflectImports(module: Type|void| |reflectProviders|reflectProviders(module: Type|void| |reflectControllers|reflectControllers(module: Type|void| |reflectDynamicMetadata|reflectDynamicMetadata(cls: Type|void| |reflectExports|reflectExports(module: Type|void| |reflectInjectables|reflectInjectables(component: Type|void| |reflectParamInjectables|reflectParamInjectables(component: Type|void| |reflectKeyMetadata|reflectKeyMetadata(component: Type|{ methodKey: string; metadata: any } | undefined| |calculateModulesDistance|calculateModulesDistance()|void| |insertImport|insertImport(related: any, token: string, context: string)|void| |isCustomProvider|isCustomProvider(provider: Provider)|provider is | ClassProvider | ValueProvider | FactoryProvider | ExistingProvider| |insertProvider|insertProvider(provider: Provider, token: string)|void| |insertInjectable|insertInjectable(injectable: Type|void| |insertExportedProviderOrModule|insertExportedProviderOrModule(toExport: ForwardReference | DynamicModule | Type|void| |insertController|insertController(controller: Type|void| |reflectMetadata|reflectMetadata(metadataKey: string, metatype: Type|T[]| |registerCoreModule|registerCoreModule(overrides: ModuleOverride[])|void| |addScopedEnhancersMetadata|addScopedEnhancersMetadata()|void| |applyApplicationProviders|applyApplicationProviders()|void| |getApplyProvidersMap|getApplyProvidersMap()|{ [type: string]: Function }| |getApplyRequestProvidersMap|getApplyRequestProvidersMap()|{ [type: string]: Function }| |isDynamicModule|isDynamicModule(module: Type|module is DynamicModule` |
Where it refuses work
DependenciesScannerstops the work withUndefinedModuleExceptionwheninnerModule === undefined.DependenciesScannerstops the work withInvalidModuleExceptionwhen!innerModule.DependenciesScannerstops the work withInvalidClassModuleExceptionwhenthis.isInjectable(moduleToAdd).DependenciesScannerstops the work withInvalidClassModuleExceptionwhenthis.isController(moduleToAdd).DependenciesScannerstops the work withInvalidClassModuleExceptionwhenthis.isExceptionFilter(moduleToAdd).DependenciesScannerstops the work withCircularDependencyExceptionwhenisUndefined(related).
Diagram
mermaidgraph LR A[Root Module] --> B[DependenciesScanner.scan] B --> C[scanForModules] C --> D[insertModule] D --> E[NestContainer] B --> F[scanModulesForDependencies] F --> G[reflectImports] F --> H[reflectProviders] F --> I[reflectControllers] F --> J[reflectDynamicMetadata] F --> K[reflectExports] F --> L[reflectInjectables] G --> E H --> E I --> E J --> E K --> E L --> E
Usage
tsimport { DependenciesScanner } from '@nestjs/core/scanner';
import { NestContainer } from '@nestjs/core/injector/container';
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
import { GraphInspector } from '@nestjs/core/inspector/graph-inspector';
import { ApplicationConfig } from '@nestjs/core/application-config';
import { AppModule } from './app.module';
// These dependencies are normally created and managed by Nest's core bootstrap flow.
declare const container: NestContainer;
declare const metadataScanner: MetadataScanner;
declare const graphInspector: GraphInspector;
declare const applicationConfig: ApplicationConfig;
async function scanApplicationModules() {
const scanner = new DependenciesScanner(
container,
metadataScanner,
graphInspector,
applicationConfig,
);
await scanner.scan(AppModule);
// The container now includes discovered modules, providers, and controllers.
}
AI Coding Instructions
- Treat
DependenciesScanneras framework bootstrap infrastructure; application code should normally rely onNestFactoryrather than instantiate it directly. - Preserve the scanning order: register modules before reflecting imports, providers, controllers, exports, and injectable metadata.
- When adding metadata reflection behavior, ensure discovered components are registered through
NestContainerso dependency resolution remains consistent. - Account for dynamic modules and module overrides when changing
scanForModules()orinsertModule(). - Avoid duplicate module registration; use the
insertedresult frominsertModule()when subsequent work should only run for newly added modules.
Relationships
- IMPORTS →
DynamicModule - IMPORTS →
ForwardReference - IMPORTS →
Provider - IMPORTS →
CATCH_WATERMARK - IMPORTS →
CONTROLLER_WATERMARK - IMPORTS →
ENHANCER_KEY_TO_SUBTYPE_MAP - IMPORTS →
EXCEPTION_FILTERS_METADATA - IMPORTS →
EnhancerSubtype - IMPORTS →
GUARDS_METADATA - IMPORTS →
INJECTABLE_WATERMARK - IMPORTS →
INTERCEPTORS_METADATA - IMPORTS →
MODULE_METADATA - IMPORTS →
PIPES_METADATA - IMPORTS →
ROUTE_ARGS_METADATA - IMPORTS →
CanActivate - IMPORTS →
ClassProvider - IMPORTS →
Controller - IMPORTS →
ExceptionFilter - IMPORTS →
ExistingProvider - IMPORTS →
FactoryProvider - IMPORTS →
Injectable - IMPORTS →
InjectionToken - IMPORTS →
NestInterceptor - IMPORTS →
PipeTransform - IMPORTS →
Scope - IMPORTS →
Type - IMPORTS →
ValueProvider - IMPORTS →
isFunction - IMPORTS →
isNil - IMPORTS →
isUndefined
Used by
1 reference from 1 file. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.
Imported by (1)
TestingModuleOptions—packages/testing/testing-module.builder.ts:29
Was this page helpful?