jestjs/jest · error · Error

Cannot find module '${presetPath}'

Error message

Cannot find module '${presetPath}'

What it means

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).

Source

Thrown at packages/jest-config/src/normalize.ts:188

const setupPreset = async (
  options: Config.InitialOptionsWithRootDir,
  optionsPreset: string,
): Promise<Config.InitialOptionsWithRootDir> => {
  let preset: Config.InitialOptions;
  const presetPath = replaceRootDirInPath(options.rootDir, optionsPreset);
  const presetModule = Resolver.findNodeModule(
    presetPath.startsWith('.') || path.isAbsolute(presetPath)
      ? presetPath
      : `${presetPath}/${PRESET_NAME}`,
    {
      basedir: options.rootDir,
      extensions: PRESET_EXTENSIONS,
    },
  );

  try {
    if (!presetModule) {
      throw new Error(`Cannot find module '${presetPath}'`);
    }

    // Force re-evaluation to support multiple projects
    try {
      delete require.cache[require.resolve(presetModule)];
    } catch {}

    preset = await requireOrImportModule(presetModule);
  } catch (error: any) {
    if (error instanceof SyntaxError || error instanceof TypeError) {
      throw createConfigError(
        `  Preset ${chalk.bold(presetPath)} is invalid:\n\n  ${
          error.message
        }\n  ${error.stack}`,
      );
    }

    if (error.message.includes('Cannot find module')) {

View on GitHub (pinned to f49721c78e)

Solutions

  1. Install the preset package: `npm install --save-dev <preset>` and confirm it appears in node_modules.
  2. Check the spelling of the preset name and that the package exports `jest-preset.js` or `jest-preset.json` at its root.
  3. If using a relative/absolute path, verify it resolves correctly against the configured `rootDir`.

Example fix

// before
module.exports = { preset: 'ts-jest' }; // not installed

// after
// npm install --save-dev ts-jest
module.exports = { preset: 'ts-jest' };
Defensive patterns

Strategy: try-catch

Validate before calling

import {Resolver} from 'jest-resolve';
function assertPresetResolvable(preset: string, rootDir: string): void {
  const candidate = preset.startsWith('.') || path.isAbsolute(preset) ? preset : `${preset}/jest-preset`;
  if (!Resolver.findNodeModule(candidate, { basedir: rootDir })) {
    throw new Error(`Preset '${preset}' not resolvable from ${rootDir}; install it or check the path`);
  }
}

Type guard

const isResolvable = (p: string, rootDir: string) =>
  Boolean(Resolver.findNodeModule(p, { basedir: rootDir }));

Try / catch

try {
  require.resolve(presetPath.endsWith('/jest-preset') ? presetPath : `${presetPath}/jest-preset`, { paths: [rootDir] });
} catch {
  throw new Error(`Preset '${presetPath}' not found; run: npm i -D ${presetPath}`);
}

Prevention

When it happens

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

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

Related errors


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