{"id":"abebc26253eb37c7","repo":"jestjs/jest","slug":"require-on-a-require-cache-esm-entry-is-not-supp","errorCode":null,"errorMessage":"require() on a require.cache ESM entry is not supported","messagePattern":"require\\(\\) on a require\\.cache ESM entry is not supported","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-runtime/src/internals/ModuleRegistries.ts","lineNumber":196,"sourceCode":"    const existing = this.esmRequireCacheWrappers.get(esm);\n    if (existing) return existing;\n    const dir = path.dirname(filename);\n    const wrapper = {\n      children: [],\n      exports: esm.namespace,\n      filename,\n      id: filename,\n      isPreloading: false,\n      loaded: true,\n      parent: null,\n      path: dir,\n      paths: (\n        nativeModule.Module as unknown as {\n          _nodeModulePaths: (from: string) => Array<string>;\n        }\n      )._nodeModulePaths(dir),\n      require: (() => {\n        throw new Error(\n          'require() on a require.cache ESM entry is not supported',\n        );\n      }) as unknown as NodeModule['require'],\n    } satisfies NodeModule;\n    this.esmRequireCacheWrappers.set(esm, wrapper);\n    return wrapper;\n  }\n\n  createRequireCacheProxy(): NodeJS.Require['cache'] {\n    const esmEntry = (key: string) => {\n      const entry = this.esModuleRegistry.get(key);\n      if (!isLiveEsm(entry)) return undefined;\n      return this.wrapEsmForRequireCache(key, entry);\n    };\n    return new Proxy<NodeJS.Require['cache']>(Object.create(null), {\n      defineProperty: notPermittedMethod,\n      deleteProperty: notPermittedMethod,\n      get: (_target, key) => {","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-runtime/src/internals/ModuleRegistries.ts#L178-L214","documentation":"Thrown synchronously when user code calls `require()` on a `NodeModule` wrapper that `wrapEsmForRequireCache` (ModuleRegistries.ts:177-203) synthesized to expose an ESM module through `require.cache`. The wrapper deliberately installs a `require` property whose getter throws, because re-requiring an already-loaded ESM entry has no defined semantics in Jest's registry.","triggerScenarios":"Iterating `Object.values(require.cache)` and calling `entry.require('./something')` on an entry whose underlying module is ESM (e.g. loaded via `import`). Reading `require.cache[filename].require('...')` where `filename` resolved as ESM.","commonSituations":"Custom test tooling that walks `require.cache` for teardown/reload logic. Mixing ESM `import` and CJS `require` of the same file in one test run.","solutions":["Don't call `require()` on entries sourced from `require.cache`; use the registry's normal `require()` from your test module instead.","If you must inspect `require.cache`, guard with `if (typeof entry.require === 'function')` — but note the wrapper types its `require` as a function that throws, so prefer checking `entry.filename`/`entry.loaded` and skip ESM entries.","Reload the ESM module via `jest.isolateModulesAsync(() => import(...))` rather than via the cache wrapper."],"exampleFix":"// before\nfor (const mod of Object.values(require.cache)) {\n  mod.require('./dep'); // throws for ESM entries\n}\n\n// after — skip cache entries that can't be re-required\nfor (const mod of Object.values(require.cache)) {\n  if (mod.require) continue; // wrapper throws; just skip\n}\n// reload via isolateModulesAsync instead","handlingStrategy":"validation","validationCode":"function safeRequirer(entry: NodeModule): boolean {\n  // require.cache wrappers for ESM entries throw on call;\n  // only treat entries that look like real CJS modules as re-requireable.\n  return Boolean(entry && entry.loaded && !entry._esmWrapper);\n}","typeGuard":null,"tryCatchPattern":"try {\n  entry.require('./dep');\n} catch (e) {\n  if (e instanceof Error && e.message.includes('require.cache ESM entry')) {\n    // skip — reload via isolateModulesAsync instead\n  } else throw e;\n}","preventionTips":["Treat require.cache as read-only introspection, not a reload mechanism.","When you must reload a module, use jest.isolateModules / isolateModulesAsync rather than reaching into the cache."],"tags":["esm","cjs-interop","require-cache"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}