jestjs/jest · error · Error

Jest: requireOrImportModule path must be absolute, was "${fi

Error message

Jest: requireOrImportModule path must be absolute, was "${filePath}"

What it means

Thrown by `requireOrImportModule` (requireOrImportModule.ts:49-53) when `filePath` starts with `.` (a relative path). The loader only handles absolute paths because `require()` of a relative string resolves against Jest's own module directory, not the caller's, producing confusing failures; Jest rejects up front instead.

Source

Thrown at packages/jest-util/src/requireOrImportModule.ts:50

    }

    return importedModule.default;
  } catch (error: any) {
    if (error.message === 'Not supported') {
      throw new Error(
        `Jest: Your version of Node does not support dynamic import - please enable it or use a .cjs file extension for file ${filePath}`,
      );
    }
    throw error;
  }
}

export default async function requireOrImportModule<T>(
  filePath: string,
  applyInteropRequireDefault = true,
): Promise<T> {
  if (!isAbsolute(filePath) && filePath[0] === '.') {
    throw new Error(
      `Jest: requireOrImportModule path must be absolute, was "${filePath}"`,
    );
  }
  try {
    if (filePath.endsWith('.mjs') || filePath.endsWith('.mts')) {
      return importModule(filePath, applyInteropRequireDefault);
    }

    const requiredModule = require(filePath);
    if (!applyInteropRequireDefault) {
      return requiredModule;
    }
    return interopRequireDefault(requiredModule).default;
  } catch (error: any) {
    if (
      error.code === 'ERR_REQUIRE_ESM' ||
      error.code === 'ERR_REQUIRE_ASYNC_MODULE'
    ) {

View on GitHub (pinned to f49721c78e)

Solutions

  1. Resolve the path to absolute before calling: `path.resolve(process.cwd(), './some/file.js')` or `path.join(__dirname, './file.js')`.
  2. If the path comes from a resolver, ensure the resolver returns absolute paths.
  3. Use `require.resolve()` from a known anchor module to get an absolute path.

Example fix

// before
await requireOrImportModule('./my-resolver.js');
// after
const path = require('path');
await requireOrImportModule(path.resolve(__dirname, './my-resolver.js'));
Defensive patterns

Strategy: validation

Validate before calling

const { isAbsolute, resolve } = require('path');
if (!isAbsolute(filePath)) filePath = resolve(filePath); // never pass a relative path

Type guard

const isAbsolutePath = (p: string): boolean => path.isAbsolute(p);

Prevention

When it happens

Trigger: Calling `requireOrImportModule('./some/relative/file.js')` or any `'./...'` / `'../...'` path. Affects Jest-internal callers (config loading, snapshot resolver, transformer, runner resolution) and any user code reusing this util.

Common situations: A custom plugin/runner that hands a relative path to `requireOrImportModule`; misconfigured resolver returning a relative path; manual calls in test setup that pass `path.join('.', 'x')` instead of an absolute path.

Related errors


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