{"id":"c31ce924815e19fd","repo":"jestjs/jest","slug":"cannot-find-module-presetpath","errorCode":null,"errorMessage":"Cannot find module '${presetPath}'","messagePattern":"Cannot find module '(.+?)'","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-config/src/normalize.ts","lineNumber":188,"sourceCode":"const setupPreset = async (\n  options: Config.InitialOptionsWithRootDir,\n  optionsPreset: string,\n): Promise<Config.InitialOptionsWithRootDir> => {\n  let preset: Config.InitialOptions;\n  const presetPath = replaceRootDirInPath(options.rootDir, optionsPreset);\n  const presetModule = Resolver.findNodeModule(\n    presetPath.startsWith('.') || path.isAbsolute(presetPath)\n      ? presetPath\n      : `${presetPath}/${PRESET_NAME}`,\n    {\n      basedir: options.rootDir,\n      extensions: PRESET_EXTENSIONS,\n    },\n  );\n\n  try {\n    if (!presetModule) {\n      throw new Error(`Cannot find module '${presetPath}'`);\n    }\n\n    // Force re-evaluation to support multiple projects\n    try {\n      delete require.cache[require.resolve(presetModule)];\n    } catch {}\n\n    preset = await requireOrImportModule(presetModule);\n  } catch (error: any) {\n    if (error instanceof SyntaxError || error instanceof TypeError) {\n      throw createConfigError(\n        `  Preset ${chalk.bold(presetPath)} is invalid:\\n\\n  ${\n          error.message\n        }\\n  ${error.stack}`,\n      );\n    }\n\n    if (error.message.includes('Cannot find module')) {","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-config/src/normalize.ts#L170-L206","documentation":"During preset loading, `setupPreset` (packages/jest-config/src/normalize.ts:170) resolves the `preset` option via the module resolver; if `Resolver.findNodeModule` returns null it throws `Cannot find module '<presetPath>'`. Presets are resolved as either a direct path or as `<name>/jest-preset` (jest-preset.js or jest-preset.json).","triggerScenarios":"`preset: 'foo-preset'` where foo-preset is not installed; `preset: './presets/base'` that does not exist relative to rootDir; a typo in the preset name; preset installed but missing its jest-preset.js/json entry file.","commonSituations":"Forgetting to `npm install` the preset; monorepo hoisting that hides the preset from Jest's basedir; renaming a preset package; rootDir misconfigured so the relative path resolves to the wrong place.","solutions":["Install the preset package: `npm install --save-dev <preset>` and confirm it appears in node_modules.","Check the spelling of the preset name and that the package exports `jest-preset.js` or `jest-preset.json` at its root.","If using a relative/absolute path, verify it resolves correctly against the configured `rootDir`."],"exampleFix":"// before\nmodule.exports = { preset: 'ts-jest' }; // not installed\n\n// after\n// npm install --save-dev ts-jest\nmodule.exports = { preset: 'ts-jest' };","handlingStrategy":"try-catch","validationCode":"import {Resolver} from 'jest-resolve';\nfunction assertPresetResolvable(preset: string, rootDir: string): void {\n  const candidate = preset.startsWith('.') || path.isAbsolute(preset) ? preset : `${preset}/jest-preset`;\n  if (!Resolver.findNodeModule(candidate, { basedir: rootDir })) {\n    throw new Error(`Preset '${preset}' not resolvable from ${rootDir}; install it or check the path`);\n  }\n}","typeGuard":"const isResolvable = (p: string, rootDir: string) =>\n  Boolean(Resolver.findNodeModule(p, { basedir: rootDir }));","tryCatchPattern":"try {\n  require.resolve(presetPath.endsWith('/jest-preset') ? presetPath : `${presetPath}/jest-preset`, { paths: [rootDir] });\n} catch {\n  throw new Error(`Preset '${presetPath}' not found; run: npm i -D ${presetPath}`);\n}","preventionTips":["Install presets as devDependencies.","Confirm jest-preset.js/json exists at the package root for local presets.","Validate rootDir so relative preset paths resolve correctly."],"tags":["jest-config","preset","module-resolution","config"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}