jestjs/jest · error · Error

require() on a require.cache ESM entry is not supported

Error message

require() on a require.cache ESM entry is not supported

What it means

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.

Source

Thrown at packages/jest-runtime/src/internals/ModuleRegistries.ts:196

    const existing = this.esmRequireCacheWrappers.get(esm);
    if (existing) return existing;
    const dir = path.dirname(filename);
    const wrapper = {
      children: [],
      exports: esm.namespace,
      filename,
      id: filename,
      isPreloading: false,
      loaded: true,
      parent: null,
      path: dir,
      paths: (
        nativeModule.Module as unknown as {
          _nodeModulePaths: (from: string) => Array<string>;
        }
      )._nodeModulePaths(dir),
      require: (() => {
        throw new Error(
          'require() on a require.cache ESM entry is not supported',
        );
      }) as unknown as NodeModule['require'],
    } satisfies NodeModule;
    this.esmRequireCacheWrappers.set(esm, wrapper);
    return wrapper;
  }

  createRequireCacheProxy(): NodeJS.Require['cache'] {
    const esmEntry = (key: string) => {
      const entry = this.esModuleRegistry.get(key);
      if (!isLiveEsm(entry)) return undefined;
      return this.wrapEsmForRequireCache(key, entry);
    };
    return new Proxy<NodeJS.Require['cache']>(Object.create(null), {
      defineProperty: notPermittedMethod,
      deleteProperty: notPermittedMethod,
      get: (_target, key) => {

View on GitHub (pinned to f49721c78e)

Solutions

  1. Don't call `require()` on entries sourced from `require.cache`; use the registry's normal `require()` from your test module instead.
  2. 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.
  3. Reload the ESM module via `jest.isolateModulesAsync(() => import(...))` rather than via the cache wrapper.

Example fix

// before
for (const mod of Object.values(require.cache)) {
  mod.require('./dep'); // throws for ESM entries
}

// after — skip cache entries that can't be re-required
for (const mod of Object.values(require.cache)) {
  if (mod.require) continue; // wrapper throws; just skip
}
// reload via isolateModulesAsync instead
Defensive patterns

Strategy: validation

Validate before calling

function safeRequirer(entry: NodeModule): boolean {
  // require.cache wrappers for ESM entries throw on call;
  // only treat entries that look like real CJS modules as re-requireable.
  return Boolean(entry && entry.loaded && !entry._esmWrapper);
}

Try / catch

try {
  entry.require('./dep');
} catch (e) {
  if (e instanceof Error && e.message.includes('require.cache ESM entry')) {
    // skip — reload via isolateModulesAsync instead
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03). Data as JSON: /data/errors/abebc26253eb37c7.json. Report an issue: GitHub.