{"id":"04a4a50f5d0f7b49","repo":"jestjs/jest","slug":"the-first-argument-to-require-resolve-paths-must-b","errorCode":null,"errorMessage":"The first argument to require.resolve.paths must be a string. Received null or undefined.","messagePattern":"The first argument to require\\.resolve\\.paths must be a string\\. Received null or undefined\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-runtime/src/internals/cjsRequire.ts","lineNumber":178,"sourceCode":"\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(\n    from: string,\n    moduleName: string | undefined,\n  ): Array<string> | null {\n    const fromDir = path.resolve(from, '..');\n    if (moduleName == null) {\n      throw new Error(\n        'The first argument to require.resolve.paths must be a string. Received null or undefined.',\n      );\n    }\n    if (moduleName.length === 0) {\n      throw new Error(\n        'The first argument to require.resolve.paths must not be the empty string.',\n      );\n    }\n\n    if (moduleName[0] === '.') {\n      return [fromDir];\n    }\n    if (this.resolution.isCoreModule(moduleName)) {\n      return null;\n    }\n    const modulePaths = this.resolution.getModulePaths(fromDir);\n    const globalPaths = this.resolution.getGlobalPaths(moduleName);\n    return [...modulePaths, ...globalPaths];","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-runtime/src/internals/cjsRequire.ts#L160-L196","documentation":"Thrown by `RequireBuilder.resolvePaths` (cjsRequire.ts:177-181) when `require.resolve.paths` is called with `null` or `undefined`. Mirrors the `require.resolve` guard: the runtime needs a defined string before computing search paths.","triggerScenarios":"`require.resolve.paths(null)` or `require.resolve.paths(someOptional)` where `someOptional` is undefined.","commonSituations":"Tooling that introspects Node's resolution paths using a variable that came from optional config or an env var.","solutions":["Validate the argument is a string first.","Provide a default module name.","Move the call behind a conditional that guarantees the value is set."],"exampleFix":"// before\nconst searchPaths = require.resolve.paths(maybeModule); // throws when undefined\n\n// after\nconst searchPaths =\n  typeof maybeModule === 'string'\n    ? require.resolve.paths(maybeModule)\n    : null;","handlingStrategy":"type-guard","validationCode":"if (typeof name !== 'string') {\n  throw new Error('require.resolve.paths needs a string');\n}\nrequire.resolve.paths(name);","typeGuard":"function isString(v: unknown): v is string {\n  return typeof v === 'string';\n}","tryCatchPattern":null,"preventionTips":["Always type introspection inputs as string at the source.","Wrap require.resolve.paths calls behind a typed helper."],"tags":["require-resolve","validation","null-check"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}