jestjs/jest · error · ModuleNotFoundError
Cannot resolve module '${moduleName}' from paths ['${paths}'
Error message
Cannot resolve module '${moduleName}' from paths ['${paths}'] from ${from} What it means
Thrown as `Resolver.ModuleNotFoundError` from `RequireBuilder.resolve` (cjsRequire.ts:154-158) when `require.resolve(name, { paths: [...] })` is used and the resolver could not find `name` under any of the supplied `paths`. Distinct from the default no-`paths` resolution path; this specifically means the explicit `paths` array was exhausted.
Source
Thrown at packages/jest-runtime/src/internals/cjsRequire.ts:154
return module;
}
for (const searchPath of options.paths) {
const absolutePath = path.resolve(from, '..', searchPath);
// required to also resolve files without leading './' directly in the path
const module = this.resolution.resolveCjsFromDirIfExists(
absolutePath,
moduleName,
[absolutePath],
);
if (module) {
return module;
}
}
throw new Resolver.ModuleNotFoundError(
`Cannot resolve module '${moduleName}' from paths ['${options.paths.join(
"', '",
)}'] from ${from}`,
);
}
try {
return this.resolution.resolveCjs(from, moduleName);
} catch (error) {
const module = this.resolution.getCjsMockModule(from, moduleName);
if (module) {
return module;
}
throw error;
}
}
private resolvePaths(View on GitHub (pinned to f49721c78e)
Solutions
- Print the absolute paths Jest searched and confirm the package is actually there: `console.log(require.resolve.paths(name))`.
- Install the missing dependency, or fix the `paths` entries to point at the real `node_modules` parent.
- Add or correct `moduleDirectories` / `roots` in Jest config so default resolution finds it without `paths`.
- If the import is a workspace package, ensure it's built/linked via the workspace tool (pnpm/yarn/npm workspaces).
Example fix
// before
require.resolve('@scope/pkg', { paths: ['./libs'] }); // not found
// after
require.resolve('@scope/pkg', { paths: [path.resolve(__dirname, '../packages')] }); Defensive patterns
Strategy: validation
Validate before calling
const fs = require('fs');
const path = require('path');
const candidatePaths = ['./libs', '../packages'].map(p => path.resolve(p));
const visible = candidatePaths.filter(p => fs.existsSync(path.join(p, moduleName)));
if (visible.length === 0) {
throw new Error(`${moduleName} not found under any of ${candidatePaths.join(', ')}`);
}
require.resolve(moduleName, { paths: candidatePaths }); Try / catch
try {
return require.resolve(moduleName, { paths });
} catch (e) {
if (e instanceof Error && e.message.startsWith('Cannot resolve module')) {
// fall back to a different search strategy or surface a friendlier error
}
throw e;
} Prevention
- Prefer configuring `moduleDirectories` and `roots` in jest.config over hand-rolled `paths` arrays.
- In monorepos, resolve through the workspace's node_modules rather than guessing relative paths.
- Log the absolute paths you pass so failures are debuggable.
When it happens
Trigger: `require.resolve('mypackage', { paths: ['/some/dir', '/other/dir'] })` where `mypackage` isn't installed under either. Also when relative paths in `paths` resolve against an unexpected `from` directory.
Common situations: Monorepo setups that hand-construct `paths` from workspace roots. CI environments where `node_modules` lives in a different location than locally. Wrong `moduleDirectories` config.
Related errors
- Cannot find module '${presetPath}'
- Could not resolve a module for a custom reporter. Module n
- The name `${name}` was looked up in the Haste module map. It
- The first argument to require.resolve must be a string. Rece
- The first argument to require.resolve.paths must be a string
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/ec3811dc74e27e69.json.
Report an issue: GitHub.