{"record":{"id":"0838327be3588d64","repo":"nestjs/nest","slug":"nest-could-not-select-the-given-module-type-na","errorCode":null,"errorMessage":"Nest could not select the given module (\"${type.name}\" does not exist in current context).","messagePattern":"Nest could not select the given module \\(\"(.+?)\" does not exist in current context\\)\\.","errorType":"exception","errorClass":"UnknownModuleException","httpStatus":null,"severity":"error","filePath":"packages/core/nest-application-context.ts","lineNumber":111,"sourceCode":"  ): INestApplicationContext {\n    const modulesContainer = this.container.getModules();\n    const contextModuleCtor = this.contextModule!.metatype;\n    const scope = this.scope.concat(contextModuleCtor);\n\n    const moduleTokenFactory = this.container.getModuleTokenFactory();\n    const { type, dynamicMetadata } =\n      this.moduleCompiler.extractMetadata(moduleType);\n    const token = dynamicMetadata\n      ? moduleTokenFactory.createForDynamic(\n          type,\n          dynamicMetadata,\n          moduleType as DynamicModule,\n        )\n      : moduleTokenFactory.createForStatic(type, moduleType as Type);\n\n    const selectedModule = modulesContainer.get(token);\n    if (!selectedModule) {\n      throw new UnknownModuleException(type.name);\n    }\n\n    const options =\n      typeof selectOptions?.abortOnError !== 'undefined'\n        ? {\n            ...this.appOptions,\n            ...selectOptions,\n          }\n        : this.appOptions;\n\n    return new NestApplicationContext(\n      this.container,\n      options,\n      selectedModule,\n      scope,\n    );\n  }\n","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/nestjs/nest/blob/dd75d7bd8c5e88048587e6768d36eb695f3e7a25/packages/core/nest-application-context.ts#L93-L129","documentation":"`app.select(Module)` / context `select()` locate a module inside the initialized modules container using a computed token (static classes hash by name+id, DynamicModules additionally hash their dynamic metadata). If the container has no module under that token, UnknownModuleException ('Nest could not select the given module (\"X\" does not exist in current context)') is thrown. The module you asked for is not part of the dependency graph you asked in.","triggerScenarios":"Selecting a module class that no imported module references; selecting the class of a DynamicModule — each instantiation registers under a dynamic token derived from its options, so `select(ConfigModule)` fails while `select(ConfigModule.forRoot().module ?? ...)` differs; calling select() on a sub-context whose scope excludes the module; module token changed between app versions or HMR reloads.","commonSituations":"Testing utilities that reach into a specific feature module; plugin code selecting the module that hosts a provider; multi-app setups (HTTP + microservice) where the module is only imported into one of them; dynamic modules configured at runtime whose selected reference is the generic class.","solutions":["Ensure the target module is reachable: it (or a module importing it) must be in the root module's `imports` tree.","For dynamic modules, select a stable static module that imports the dynamic one, or fetch providers with `app.get(Token, { strict: false })` instead.","Run `select()` only after `NestFactory.create()` / `app.init()` has finished.","Verify you are calling select on the same application instance that registered the module (not a different sub-context)."],"exampleFix":"// before\nconst ctx = app.select(FeatureModule); // never imported anywhere\n\n// after\n@Module({ imports: [FeatureModule] }) export class AppModule {}\n// ...\nconst ctx = app.select(FeatureModule);\nconst svc = ctx.get(FeatureService, { strict: false });","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Prefer strict selection, degrade to app-wide provider lookup\nlet ctx: INestApplicationContext;\ntry {\n  ctx = app.select(FeatureModule);\n} catch {\n  ctx = app; // module not selectable — search everywhere instead\n}\nconst service = ctx.get(FeatureService, { strict: false });","preventionTips":["Import every module you plan to select() into the root module tree (directly or transitively).","For dynamic modules, avoid select() on the generic class — select a stable static module or use get with strict:false.","Perform select() only after app.init()/NestFactory.create resolves."],"tags":["moduleref","module-selection","dynamic-module","di-container"],"backgroundTag":"module-not-found","analyzedSha":"dd75d7bd8c5e88048587e6768d36eb695f3e7a25","analyzedAt":"2026-08-21T19:39:39.867Z","contentChangedAt":"2026-08-21T19:39:39.867Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}