Kind: Class
Source: packages/core/injector/injector.ts
Part of: Core
Injector is Nest’s internal dependency-injection runtime responsible for creating class, factory, controller, middleware, and provider instances. It resolves constructor dependencies, tracks circular or asynchronous resolution through settlement signals, and stores resolved instances in their module collections and execution contexts.
Methods
| Method | Signature | Returns |
|---|---|---|
loadPrototype | loadPrototype({ token }: InstanceWrapper<T>, collection: Map<InjectionToken, InstanceWrapper<T>>, contextId: undefined) | void |
loadInstance | loadInstance(wrapper: InstanceWrapper<T>, collection: Map<InjectionToken, InstanceWrapper>, moduleRef: Module, resolutionContext: ResolutionContext) | void |
loadMiddleware | loadMiddleware(wrapper: InstanceWrapper, collection: Map<InjectionToken, InstanceWrapper>, moduleRef: Module, contextId: undefined, inquirer: InstanceWrapper) | void |
loadController | loadController(wrapper: InstanceWrapper<Controller>, moduleRef: Module, contextId: undefined) | void |
loadInjectable | loadInjectable(wrapper: InstanceWrapper<T>, moduleRef: Module, contextId: undefined, inquirer: InstanceWrapper) | void |
loadProvider | loadProvider(wrapper: InstanceWrapper<Injectable>, moduleRef: Module, resolutionContext: ResolutionContext) | void |
applySettlementSignal | applySettlementSignal(instancePerContext: InstancePerContext<T>, host: InstanceWrapper<T>) | void |
resolveConstructorParams | `resolveConstructorParams(wrapper: InstanceWrapper | undefined, callback: (args: unknown[]) => void |
getClassDependencies | getClassDependencies(wrapper: InstanceWrapper<T>) | [InjectorDependency[], number[]] |
getFactoryProviderDependencies | getFactoryProviderDependencies(wrapper: InstanceWrapper<T>) | [InjectorDependency[], number[]] |
reflectConstructorParams | `reflectConstructorParams(type: Type | Function)` |
reflectOptionalParams | `reflectOptionalParams(type: Type | Function)` |
reflectSelfParams | `reflectSelfParams(type: Type | Function)` |
resolveSingleParam | `resolveSingleParam(wrapper: InstanceWrapper | string |
resolveParamToken | `resolveParamToken(wrapper: InstanceWrapper | string |
resolveComponentWrapper | `resolveComponentWrapper(moduleRef: Module, token: InjectionToken, dependencyContext: InjectorDependencyContext, wrapper: InstanceWrapper | string |
resolveComponentHost | `resolveComponentHost(moduleRef: Module, instanceWrapper: InstanceWrapper<T | Promise |
lookupComponent | `lookupComponent(providers: Map<Function | string |
lookupComponentInParentModules | `lookupComponentInParentModules(dependencyContext: InjectorDependencyContext, moduleRef: Module, wrapper: InstanceWrapper | string |
lookupComponentInImports | `lookupComponentInImports(moduleRef: Module, name: InjectionToken, wrapper: InstanceWrapper, moduleRegistry: Set | string |
resolveProperties | resolveProperties(wrapper: InstanceWrapper<T>, moduleRef: Module, inject: InjectorDependency[], resolutionContext: ResolutionContext, parentInquirer: InstanceWrapper) | Promise<PropertyDependency[]> |
reflectProperties | reflectProperties(type: Type<T>) | PropertyDependency[] |
applyProperties | applyProperties(instance: T, properties: PropertyDependency[]) | void |
instantiateClass | instantiateClass(instances: any[], wrapper: InstanceWrapper, targetMetatype: InstanceWrapper, resolutionContext: ResolutionContext) | Promise<T> |
loadPerContext | loadPerContext(instance: T, moduleRef: Module, collection: Map<InjectionToken, InstanceWrapper>, ctx: ContextId, wrapper: InstanceWrapper) | Promise<T> |
loadEnhancersPerContext | loadEnhancersPerContext(wrapper: InstanceWrapper, ctx: ContextId, inquirer: InstanceWrapper) | void |
loadCtorMetadata | loadCtorMetadata(metadata: InstanceWrapper<any>[], contextId: ContextId, inquirer: InstanceWrapper, parentInquirer: InstanceWrapper) | Promise<any[]> |
loadPropertiesMetadata | loadPropertiesMetadata(metadata: PropertyMetadata[], contextId: ContextId, inquirer: InstanceWrapper) | Promise<PropertyDependency[]> |
addDependencyMetadata | `addDependencyMetadata(keyOrIndex: symbol | string |
Where it refuses work
Injectorstops the work withCircularDependencyExceptionwhenresolutionContext.inquirer && settlementSignal?.isCycle(resolutionContext.inquirer.id).Injectorstops the work withRuntimeExceptionwhenisUndefined(targetWrapper).Injectorstops the work withUnknownDependenciesExceptionwhenwrapper && token === name.Injectorstops the work withUnknownDependenciesExceptionwhenisNil(instanceWrapper).Injectorstops the work with an early return when!this.isDebugMode(), in 3 places.Injectorstops the work with an early return when!collection.
When something fails
Injectorhandles failure in 3 places: it lets it reach the caller in all 3.
Diagram
mermaidgraph LR A[Module collections] --> B[Injector] B --> C[loadProvider / loadInjectable] B --> D[loadController / loadMiddleware] B --> E[loadPrototype] C --> F[resolveConstructorParams] D --> F F --> G[getClassDependencies] F --> H[getFactoryProviderDependencies] G --> I[Instantiate class or factory] H --> I I --> J[applySettlementSignal] J --> K[Resolved instance in InstanceWrapper]
Usage
tsimport { Injector } from '@nestjs/core/injector/injector';
import { Module } from '@nestjs/core/injector/module';
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
// This is typically performed by Nest internals during application bootstrap.
async function loadCustomProvider(
moduleRef: Module,
providerWrapper: InstanceWrapper,
) {
const injector = new Injector();
// Resolves constructor dependencies and creates the provider instance.
await injector.loadProvider(providerWrapper, moduleRef);
return providerWrapper.instance;
}
AI Coding Instructions
- Treat
Injectoras framework-internal infrastructure; prefer Nest’s public module and provider APIs in application code. - Use
loadProvider,loadInjectable,loadController, andloadMiddlewarewith the matchingInstanceWrappercollection owned by aModule. - Preserve the
contextIdandinquirerarguments when working with request-scoped or transient providers. - Do not manually bypass
resolveConstructorParamsorapplySettlementSignal; they handle dependency ordering, async resolution, and circular dependency coordination. - When adding provider types, ensure both class-based and factory-provider dependency paths are represented in
getClassDependenciesorgetFactoryProviderDependencies.
Used by
4 references from 4 files. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.
Imported by (4)
ListenersController—packages/microservices/listeners-controller.ts:45MicroservicesModule—packages/microservices/microservices-module.ts:23NestMicroservice—packages/microservices/nest-microservice.ts:35TestingInjector—packages/testing/testing-injector.ts:23
Was this page helpful?