{"id":"dee2eb0dd1942489","repo":"jestjs/jest","slug":"jest-requireorimportmodule-path-must-be-absolute","errorCode":null,"errorMessage":"Jest: requireOrImportModule path must be absolute, was \"${filePath}\"","messagePattern":"Jest: requireOrImportModule path must be absolute, was \"(.+?)\"","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-util/src/requireOrImportModule.ts","lineNumber":50,"sourceCode":"    }\n\n    return importedModule.default;\n  } catch (error: any) {\n    if (error.message === 'Not supported') {\n      throw new Error(\n        `Jest: Your version of Node does not support dynamic import - please enable it or use a .cjs file extension for file ${filePath}`,\n      );\n    }\n    throw error;\n  }\n}\n\nexport default async function requireOrImportModule<T>(\n  filePath: string,\n  applyInteropRequireDefault = true,\n): Promise<T> {\n  if (!isAbsolute(filePath) && filePath[0] === '.') {\n    throw new Error(\n      `Jest: requireOrImportModule path must be absolute, was \"${filePath}\"`,\n    );\n  }\n  try {\n    if (filePath.endsWith('.mjs') || filePath.endsWith('.mts')) {\n      return importModule(filePath, applyInteropRequireDefault);\n    }\n\n    const requiredModule = require(filePath);\n    if (!applyInteropRequireDefault) {\n      return requiredModule;\n    }\n    return interopRequireDefault(requiredModule).default;\n  } catch (error: any) {\n    if (\n      error.code === 'ERR_REQUIRE_ESM' ||\n      error.code === 'ERR_REQUIRE_ASYNC_MODULE'\n    ) {","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-util/src/requireOrImportModule.ts#L32-L68","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Resolve the path to absolute before calling: `path.resolve(process.cwd(), './some/file.js')` or `path.join(__dirname, './file.js')`.","If the path comes from a resolver, ensure the resolver returns absolute paths.","Use `require.resolve()` from a known anchor module to get an absolute path."],"exampleFix":"// before\nawait requireOrImportModule('./my-resolver.js');\n// after\nconst path = require('path');\nawait requireOrImportModule(path.resolve(__dirname, './my-resolver.js'));","handlingStrategy":"validation","validationCode":"const { isAbsolute, resolve } = require('path');\nif (!isAbsolute(filePath)) filePath = resolve(filePath); // never pass a relative path","typeGuard":"const isAbsolutePath = (p: string): boolean => path.isAbsolute(p);","tryCatchPattern":null,"preventionTips":["Always pass absolute paths to requireOrImportModule - use `path.resolve`/`path.join(__dirname, ...)`.","Resolvers you plug into Jest must return absolute paths.","Reject relative paths at the boundary of your own helpers."],"tags":["jest-util","module-loading","paths","user-error"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}