{"record":{"id":"2734e367d0332ba3","repo":"nestjs/nest","slug":"nest-cannot-create-the-parentmodule-name-instan-2734e3","errorCode":null,"errorMessage":"Nest cannot create the ${parentModule.name} instance.\nReceived an unexpected value at index [${index}] of the ${parentModule.name} \"imports\" array.\nThe received value `${formattedValue}` is of type \"${receivedType}\".\n\nScope [${scope}]","messagePattern":"Nest cannot create the (.+?) instance\\.\nReceived an unexpected value at index \\[(.+?)\\] of the (.+?) \"imports\" array\\.\nThe received value `(.+?)` is of type \"(.+?)\"\\.\n\nScope \\[(.+?)\\]","errorType":"exception","errorClass":"InvalidModuleException","httpStatus":null,"severity":"critical","filePath":"packages/core/scanner.ts","lineNumber":155,"sourceCode":"          MODULE_METADATA.IMPORTS,\n          moduleDefinition as Type<any>,\n        )\n      : [\n          ...this.reflectMetadata(\n            MODULE_METADATA.IMPORTS,\n            (moduleDefinition as DynamicModule).module,\n          ),\n          ...((moduleDefinition as DynamicModule).imports || []),\n        ];\n\n    let registeredModuleRefs: Module[] = [];\n    for (const [index, innerModule] of modules.entries()) {\n      // In case of a circular dependency (ES module system), JavaScript will resolve the type to `undefined`.\n      if (innerModule === undefined) {\n        throw new UndefinedModuleException(moduleDefinition, index, scope);\n      }\n      if (!innerModule) {\n        throw new InvalidModuleException(\n          moduleDefinition,\n          index,\n          scope,\n          innerModule,\n        );\n      }\n      if (ctxRegistry.includes(innerModule)) {\n        continue;\n      }\n      const moduleRefs = await this.scanForModules({\n        moduleDefinition: innerModule,\n        scope: ([] as Array<Type>).concat(scope, moduleDefinition as Type),\n        ctxRegistry,\n        overrides,\n        lazy,\n      });\n      registeredModuleRefs = registeredModuleRefs.concat(moduleRefs);\n    }","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/nestjs/nest/blob/dd75d7bd8c5e88048587e6768d36eb695f3e7a25/packages/core/scanner.ts#L137-L173","documentation":"The scanner distinguishes `undefined` (circular-import signature) from other falsy values: any null, empty string, 0, false or NaN in a module's `imports` array raises InvalidModuleException ('Received an unexpected value at index [i] of the imports array... The received value X is of type Y'). It means an expression in the array evaluated to a non-module falsy value — a data/config artifact rather than a module class.","triggerScenarios":"`imports: [config?.module]` where config is null; `imports: [...(features ?? [])]` with an array containing nulls; destructured dynamic module config that yields `''` or `0` when a provider returns early; unit-test module builders receiving unstubbed values; `process.env.FLAG && FeatureModule` evaluating to false and being kept in the array.","commonSituations":"Optional dynamic modules assembled from configuration objects; feature-flagged imports written as `flag && Module` (false leaks into the array); mocks in tests returning null for module factories; refactors where a getter used to build imports returns empty values on edge cases.","solutions":["Find the offending index in the error message and inspect the exact expression at that position in the `imports` array.","Replace `flag && FeatureModule` with a conditional spread: `imports: [...(flag ? [FeatureModule] : [])]`.","Sanitize assembled arrays: `imports: importedModules.filter(Boolean)` when the array is built from config.","Give optional config a real default (`?? []`, `?? {}`) so the expression yields a module or nothing at all, never null/false."],"exampleFix":"// before\n@Module({\n  imports: [process.env.USE_FEATURE && FeatureModule, CoreModule], // false at index 0\n})\n\n// after\n@Module({\n  imports: [\n    ...(process.env.USE_FEATURE ? [FeatureModule] : []),\n    CoreModule,\n  ],\n})","handlingStrategy":"validation","validationCode":"// Reject any non-module (falsy or otherwise) entry before Nest sees it\nconst isNestModuleLike = (v: any): boolean =>\n  typeof v === 'function' && !!v.prototype;\n\nfunction sanitizeImports(imports: unknown[]): any[] {\n  const invalid = imports.filter(v => !isNestModuleLike(v));\n  if (invalid.length > 0) {\n    throw new Error(`Non-module entries in imports: ${invalid.map(String).join(', ')}`);\n  }\n  return imports.filter(Boolean) as any[];\n}\n\n// const dynamicImports = sanitizeImports([config?.module, FeatureModule]);","typeGuard":"const isModuleClass = (v: unknown): v is abstract new (...a: any[]) => any =>\n  typeof v === 'function' && typeof (v as any).prototype === 'object';","tryCatchPattern":null,"preventionTips":["Write feature toggles as conditional spreads, never `flag && Module` inline in the array.","Default optional config with `?? []` so builders never emit null/false entries.","Validate assembled dynamic imports with the isModuleClass guard in a wiring test."],"tags":["module-configuration","dynamic-module","falsy-value","bootstrap"],"backgroundTag":"invalid-module-import","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"}