{"record":{"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/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/jest-config/src/normalize.ts#L170-L206","documentation":"setupPreset in normalize.ts (lines 170-237) resolves the preset name via Resolver.findNodeModule and requires the result. If findNodeModule returns null (line 187), it throws this raw 'Cannot find module' error; the surrounding catch (lines 197-236) then rewraps it into a more helpful createConfigError that distinguishes missing-preset, missing-jest-preset-file, and missing-dependency cases. The raw message surfaces when the catch hasn't yet translated it.","triggerScenarios":"Setting preset: 'foo' where 'foo' is not installed; preset path with a typo; preset resolving against the wrong rootDir; relative preset path that doesn't exist; a preset whose package.json lacks a jest-preset.js/json entry.","commonSituations":"Forgetting to npm install the preset package; monorepo rootDir misconfiguration so resolution happens from the wrong base; upgrading and removing a preset dependency; scoped package name typo (e.g. @scope/preset vs @scope/jest-preset).","solutions":["Install the preset package: npm install --save-dev <preset-name>.","Verify the preset exports jest-preset.js or jest-preset.json at its root.","Check rootDir in your config — preset paths resolve relative to it.","For local/relative presets, use a path starting with ./ or an absolute path."],"exampleFix":"// before\nmodule.exports = { preset: 'my-preset' }; // not installed\n// after\nnpm install --save-dev my-preset\n// or relative:\nmodule.exports = { preset: './my-preset' };","handlingStrategy":"validation","validationCode":"import { Resolver } from 'jest-resolve';\nfunction ensurePresetExists(rootDir: string, preset: string): void {\n  const path = preset.startsWith('.') || require('node:path').isAbsolute(preset) ? preset : `${preset}/jest-preset`;\n  const mod = Resolver.findNodeModule(path, { basedir: rootDir });\n  if (!mod) throw new Error(`Preset '${preset}' not resolvable from ${rootDir}`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  await normalize(options, argv, configPath, ...);\n} catch (e) {\n  if (/Cannot find module/.test((e as Error).message)) {\n    console.error('Preset missing — did you install it?', e);\n  }\n  throw e;\n}","preventionTips":["Install preset packages as devDependencies before referencing them.","For local presets, use relative or absolute paths rooted at <rootDir>.","Verify the preset ships jest-preset.js or jest-preset.json."],"tags":["jest-config","preset","module-resolution","dependency"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}