Skip to content

Injector

reference
2 min readUpdated

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

MethodSignatureReturns
loadPrototypeloadPrototype({ token }: InstanceWrapper<T>, collection: Map<InjectionToken, InstanceWrapper<T>>, contextId: undefined)void
loadInstanceloadInstance(wrapper: InstanceWrapper<T>, collection: Map<InjectionToken, InstanceWrapper>, moduleRef: Module, resolutionContext: ResolutionContext)void
loadMiddlewareloadMiddleware(wrapper: InstanceWrapper, collection: Map<InjectionToken, InstanceWrapper>, moduleRef: Module, contextId: undefined, inquirer: InstanceWrapper)void
loadControllerloadController(wrapper: InstanceWrapper<Controller>, moduleRef: Module, contextId: undefined)void
loadInjectableloadInjectable(wrapper: InstanceWrapper<T>, moduleRef: Module, contextId: undefined, inquirer: InstanceWrapper)void
loadProviderloadProvider(wrapper: InstanceWrapper<Injectable>, moduleRef: Module, resolutionContext: ResolutionContext)void
applySettlementSignalapplySettlementSignal(instancePerContext: InstancePerContext<T>, host: InstanceWrapper<T>)void
resolveConstructorParams`resolveConstructorParams(wrapper: InstanceWrapper, moduleRef: Module, inject: InjectorDependency[]undefined, callback: (args: unknown[]) => void
getClassDependenciesgetClassDependencies(wrapper: InstanceWrapper<T>)[InjectorDependency[], number[]]
getFactoryProviderDependenciesgetFactoryProviderDependencies(wrapper: InstanceWrapper<T>)[InjectorDependency[], number[]]
reflectConstructorParams`reflectConstructorParams(type: TypeFunction)`
reflectOptionalParams`reflectOptionalParams(type: TypeFunction)`
reflectSelfParams`reflectSelfParams(type: TypeFunction)`
resolveSingleParam`resolveSingleParam(wrapper: InstanceWrapper, param: Typestring
resolveParamToken`resolveParamToken(wrapper: InstanceWrapper, param: Typestring
resolveComponentWrapper`resolveComponentWrapper(moduleRef: Module, token: InjectionToken, dependencyContext: InjectorDependencyContext, wrapper: InstanceWrapper, resolutionContext: ResolutionContext, keyOrIndex: symbolstring
resolveComponentHost`resolveComponentHost(moduleRef: Module, instanceWrapper: InstanceWrapper<TPromise>, resolutionContext: ResolutionContext)`
lookupComponent`lookupComponent(providers: Map<Functionstring
lookupComponentInParentModules`lookupComponentInParentModules(dependencyContext: InjectorDependencyContext, moduleRef: Module, wrapper: InstanceWrapper, resolutionContext: ResolutionContext, keyOrIndex: symbolstring
lookupComponentInImports`lookupComponentInImports(moduleRef: Module, name: InjectionToken, wrapper: InstanceWrapper, moduleRegistry: Set, resolutionContext: ResolutionContext, keyOrIndex: symbolstring
resolvePropertiesresolveProperties(wrapper: InstanceWrapper<T>, moduleRef: Module, inject: InjectorDependency[], resolutionContext: ResolutionContext, parentInquirer: InstanceWrapper)Promise<PropertyDependency[]>
reflectPropertiesreflectProperties(type: Type<T>)PropertyDependency[]
applyPropertiesapplyProperties(instance: T, properties: PropertyDependency[])void
instantiateClassinstantiateClass(instances: any[], wrapper: InstanceWrapper, targetMetatype: InstanceWrapper, resolutionContext: ResolutionContext)Promise<T>
loadPerContextloadPerContext(instance: T, moduleRef: Module, collection: Map<InjectionToken, InstanceWrapper>, ctx: ContextId, wrapper: InstanceWrapper)Promise<T>
loadEnhancersPerContextloadEnhancersPerContext(wrapper: InstanceWrapper, ctx: ContextId, inquirer: InstanceWrapper)void
loadCtorMetadataloadCtorMetadata(metadata: InstanceWrapper<any>[], contextId: ContextId, inquirer: InstanceWrapper, parentInquirer: InstanceWrapper)Promise<any[]>
loadPropertiesMetadataloadPropertiesMetadata(metadata: PropertyMetadata[], contextId: ContextId, inquirer: InstanceWrapper)Promise<PropertyDependency[]>
addDependencyMetadata`addDependencyMetadata(keyOrIndex: symbolstring

Where it refuses work

  • Injector stops the work with CircularDependencyException when resolutionContext.inquirer && settlementSignal?.isCycle(resolutionContext.inquirer.id).
  • Injector stops the work with RuntimeException when isUndefined(targetWrapper).
  • Injector stops the work with UnknownDependenciesException when wrapper && token === name.
  • Injector stops the work with UnknownDependenciesException when isNil(instanceWrapper).
  • Injector stops the work with an early return when !this.isDebugMode(), in 3 places.
  • Injector stops the work with an early return when !collection.

When something fails

  • Injector handles failure in 3 places: it lets it reach the caller in all 3.

Diagram

mermaid
graph 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

ts
import { 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 Injector as framework-internal infrastructure; prefer Nest’s public module and provider APIs in application code.
  • Use loadProvider, loadInjectable, loadController, and loadMiddleware with the matching InstanceWrapper collection owned by a Module.
  • Preserve the contextId and inquirer arguments when working with request-scoped or transient providers.
  • Do not manually bypass resolveConstructorParams or applySettlementSignal; 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 getClassDependencies or getFactoryProviderDependencies.

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)

  • ListenersControllerpackages/microservices/listeners-controller.ts:45
  • MicroservicesModulepackages/microservices/microservices-module.ts:23
  • NestMicroservicepackages/microservices/nest-microservice.ts:35
  • TestingInjectorpackages/testing/testing-injector.ts:23

Was this page helpful?

Download as PDF
Injector — NestJS head-to-head