{"record":{"id":"6befcdd633daa98a","repo":"nestjs/nest","slug":"moduleref-cannot-instantiate-class-value-is-no","errorCode":null,"errorMessage":"ModuleRef cannot instantiate class (${value} is not constructable).","messagePattern":"ModuleRef cannot instantiate class \\((.+?) is not constructable\\)\\.","errorType":"exception","errorClass":"InvalidClassException","httpStatus":null,"severity":"error","filePath":"packages/core/injector/module.ts","lineNumber":651,"sourceCode":"        options: ModuleRefGetOrResolveOpts = {},\n      ): Promise<TResult | Array<TResult>> {\n        options.strict ??= true;\n        options.each ??= false;\n\n        return this.resolvePerContext<TInput, TResult>(\n          typeOrToken,\n          self,\n          contextId,\n          options,\n        );\n      }\n\n      public async create<T = any>(\n        type: Type<T>,\n        contextId?: ContextId,\n      ): Promise<T> {\n        if (!(type && isFunction(type) && type.prototype)) {\n          throw new InvalidClassException(type);\n        }\n        return this.instantiateClass<T>(type, self, contextId);\n      }\n    };\n  }\n\n  private isEntryProvider(metatype: InjectionToken): boolean {\n    return typeof metatype === 'function'\n      ? !!Reflect.getMetadata(ENTRY_PROVIDER_WATERMARK, metatype)\n      : false;\n  }\n\n  private generateUuid(): string {\n    const prefix = 'M_';\n    const key = this.token\n      ? this.token.includes(':')\n        ? this.token.split(':')[1]\n        : this.token","sourceCodeStart":633,"sourceCodeEnd":669,"githubUrl":"https://github.com/nestjs/nest/blob/a3a31b96436a0c5fc33266a19c02cfa4fd2c03c0/packages/core/injector/module.ts#L633-L669","documentation":"`ModuleRef.create(type)` instantiates a class that does NOT need to be registered in the DI container, but the argument must be a real constructor: a function with a prototype. InvalidClassException ('ModuleRef cannot instantiate class (X is not constructable)') means the value passed is undefined, a string token, an arrow function without prototype data, or an interface that compiled away to nothing.","triggerScenarios":"Calling `moduleRef.create(SomeInterface)` where the 'type' is only a TypeScript type; passing a variable that resolved to `undefined` (circular import or missing import); passing a string/symbol token used with `@Inject`; passing an already-created instance or a plain object; passing a lazy `() => Class` instead of the class itself.","commonSituations":"Dynamically instantiating strategies/handlers by class reference stored in config maps; code generators emitting type-only imports (`import type`); refactors where the class import got dropped but the reference remained typed as any.","solutions":["Pass the concrete imported class reference itself: `moduleRef.create(MyJobHandler)`.","Ensure the import is a value import, not `import type` / interface-only, and that it is not undefined due to a circular import.","If you only hold a string/symbol token, resolve a registered provider with `moduleRef.get(token)` instead of `create`.","For arrow-function factories, register them as `{ provide, useFactory }` and let the container call them."],"exampleFix":"// before\nconst handler = await this.moduleRef.create('email-job' as any);\n\n// after\nimport { EmailJobHandler } from './jobs/email-job.handler';\nconst handler = await this.moduleRef.create(EmailJobHandler);","handlingStrategy":"type-guard","validationCode":"const isConstructable = (v: any): v is new (...args: any[]) => any =>\n  typeof v === 'function' && typeof v.prototype === 'object';\n\nif (!isConstructable(targetType)) {\n  throw new Error(`Refusing moduleRef.create on non-class ${String(targetType)}`);\n}\nconst instance = await this.moduleRef.create(targetType);","typeGuard":"declare const TypeBrand: unique symbol;\nfunction isConcreteClass<T>(v: unknown): v is abstract new (...args: any[]) => T {\n  return typeof v === 'function' && !!(v as any).prototype;\n}","tryCatchPattern":null,"preventionTips":["Type the helper API as accepting `Type<T>` (newable) rather than any so the compiler rejects tokens/interfaces.","Avoid `import type` for classes passed to create(); the value must exist at runtime.","In strategy maps, store class references and run the isConstructable guard once at module load."],"tags":["dependency-injection","moduleref","dynamic-instantiation","typescript"],"backgroundTag":"invalid-di-token","analyzedSha":"a3a31b96436a0c5fc33266a19c02cfa4fd2c03c0","analyzedAt":"2026-08-21T19:39:39.867Z","contentChangedAt":"2026-08-21T19:39:39.867Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}