{"id":"ec3811dc74e27e69","repo":"jestjs/jest","slug":"cannot-resolve-module-modulename-from-paths","errorCode":null,"errorMessage":"Cannot resolve module '${moduleName}' from paths ['${paths}'] from ${from}","messagePattern":"Cannot resolve module '(.+?)' from paths \\['(.+?)'\\] from (.+?)","errorType":"exception","errorClass":"ModuleNotFoundError","httpStatus":null,"severity":"error","filePath":"packages/jest-runtime/src/internals/cjsRequire.ts","lineNumber":154,"sourceCode":"        return module;\n      }\n\n      for (const searchPath of options.paths) {\n        const absolutePath = path.resolve(from, '..', searchPath);\n\n        // required to also resolve files without leading './' directly in the path\n        const module = this.resolution.resolveCjsFromDirIfExists(\n          absolutePath,\n          moduleName,\n          [absolutePath],\n        );\n\n        if (module) {\n          return module;\n        }\n      }\n\n      throw new Resolver.ModuleNotFoundError(\n        `Cannot resolve module '${moduleName}' from paths ['${options.paths.join(\n          \"', '\",\n        )}'] from ${from}`,\n      );\n    }\n\n    try {\n      return this.resolution.resolveCjs(from, moduleName);\n    } catch (error) {\n      const module = this.resolution.getCjsMockModule(from, moduleName);\n      if (module) {\n        return module;\n      }\n      throw error;\n    }\n  }\n\n  private resolvePaths(","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-runtime/src/internals/cjsRequire.ts#L136-L172","documentation":"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.","triggerScenarios":"`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.","commonSituations":"Monorepo setups that hand-construct `paths` from workspace roots. CI environments where `node_modules` lives in a different location than locally. Wrong `moduleDirectories` config.","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)."],"exampleFix":"// before\nrequire.resolve('@scope/pkg', { paths: ['./libs'] }); // not found\n\n// after\nrequire.resolve('@scope/pkg', { paths: [path.resolve(__dirname, '../packages')] });","handlingStrategy":"validation","validationCode":"const fs = require('fs');\nconst path = require('path');\nconst candidatePaths = ['./libs', '../packages'].map(p => path.resolve(p));\nconst visible = candidatePaths.filter(p => fs.existsSync(path.join(p, moduleName)));\nif (visible.length === 0) {\n  throw new Error(`${moduleName} not found under any of ${candidatePaths.join(', ')}`);\n}\nrequire.resolve(moduleName, { paths: candidatePaths });","typeGuard":null,"tryCatchPattern":"try {\n  return require.resolve(moduleName, { paths });\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('Cannot resolve module')) {\n    // fall back to a different search strategy or surface a friendlier error\n  }\n  throw e;\n}","preventionTips":["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."],"tags":["require-resolve","module-resolution","paths"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}