{"record":{"id":"6ff2ec5dd9fab683","repo":"nestjs/nest","slug":"nest-could-not-find-name-element-this-provider-6ff2ec","errorCode":null,"errorMessage":"Nest could not find ${name} element (this provider does not exist in the current context)","messagePattern":"Nest could not find (.+?) element \\(this provider does not exist in the current context\\)","errorType":"exception","errorClass":"UnknownElementException","httpStatus":null,"severity":"error","filePath":"packages/core/injector/instance-links-host.ts","lineNumber":36,"sourceCode":"  private readonly instanceLinks = new Map<InjectionToken, InstanceLink[]>();\n\n  constructor(private readonly container: NestContainer) {\n    this.initialize();\n  }\n\n  get<T = any>(token: InjectionToken): InstanceLink<T>;\n  get<T = any>(\n    token: InjectionToken,\n    options?: { moduleId?: string; each?: boolean },\n  ): InstanceLink<T> | Array<InstanceLink<T>>;\n  get<T = any>(\n    token: InjectionToken,\n    options: { moduleId?: string; each?: boolean } = {},\n  ): InstanceLink<T> | Array<InstanceLink<T>> {\n    const instanceLinksForGivenToken = this.instanceLinks.get(token);\n\n    if (!instanceLinksForGivenToken) {\n      throw new UnknownElementException(this.getInstanceNameByToken(token));\n    }\n\n    if (options.each) {\n      return instanceLinksForGivenToken;\n    }\n\n    const instanceLink = options.moduleId\n      ? instanceLinksForGivenToken.find(\n          item => item.moduleId === options.moduleId,\n        )\n      : instanceLinksForGivenToken[instanceLinksForGivenToken.length - 1];\n\n    if (!instanceLink) {\n      throw new UnknownElementException(this.getInstanceNameByToken(token));\n    }\n    return instanceLink;\n  }\n","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/nestjs/nest/blob/dd75d7bd8c5e88048587e6768d36eb695f3e7a25/packages/core/injector/instance-links-host.ts#L18-L54","documentation":"Raised by the InstanceLinksHost that backs ModuleRef lookups: `get(token)` found no entry for the token in the map of all registered instance links, so the token is not registered anywhere in the application (or the sub-application you are querying). NestJS wraps this as UnknownElementException — 'Nest could not find X element (this provider does not exist in the current context)'. Unlike the injector error, this one happens when you explicitly request an instance.","triggerScenarios":"`moduleRef.get(Token)` / `moduleRef.resolve(Token)` where no module registers that token; the token is registered only inside a lazily created context (request-scoped) while you query the static context; querying a sub-application created via `app.select()` whose scope excludes the provider; token mismatch — requesting by class while registered under a string token or vice versa; calling get() before the app is initialized.","commonSituations":"Dynamic plugin systems looking up optional features by token; standalone app contexts (worker/CLI) reusing modules that reference HTTP-only providers; requesting a provider from a different DI scope; typos in string tokens; unit tests creating a testing module without the provider being fetched.","solutions":["Verify the provider is actually registered: it must appear in some module's `providers` reachable from the root module.","Pass `{ strict: false }` to `moduleRef.get(Token, { strict: false })` to search the whole application tree instead of the current module scope.","For string/symbol registrations, request with the identical token value used at registration.","If the lookup happens in a sub-context (`app.select(...)`), either import the needed module there or query the root context.","Ensure the call happens after `NestFactory.create`/`app.init()` has completed."],"exampleFix":"// before\nconst svc = this.moduleRef.get('PAYMENT_GATEWAY'); // registered under class token\n\n// after\nconst svc = this.moduleRef.get(PaymentGatewayService, { strict: false });","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"import { UnknownElementException } from '@nestjs/core';\n\nfunction getOrGlobal<T>(moduleRef: ModuleRef, token: Type<T> | string): T {\n  try {\n    return moduleRef.get(token, { strict: true });\n  } catch (e) {\n    if (!(e instanceof UnknownElementException)) throw e;\n  }\n  return moduleRef.get(token, { strict: false }); // fall back to app-wide lookup\n}","preventionTips":["For dynamic/optional lookups, prefer get(token, { strict: false }) from the start.","Register shared tokens in a dedicated SharedModule imported everywhere so lookups never miss.","Wrap plugin-style lookups in helpers that catch UnknownElementException and degrade gracefully.","Keep registration and lookup on the identical token constant (single source of truth)."],"tags":["dependency-injection","moduleref","token","lookup"],"backgroundTag":"di-provider-not-found","analyzedSha":"dd75d7bd8c5e88048587e6768d36eb695f3e7a25","analyzedAt":"2026-08-21T19:39:39.867Z","contentChangedAt":"2026-08-21T19:39:39.867Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}