{"id":"846158abff1730f3","repo":"jestjs/jest","slug":"the-first-argument-to-require-resolve-must-be-a-st","errorCode":null,"errorMessage":"The first argument to require.resolve must be a string. Received null or undefined.","messagePattern":"The first argument to require\\.resolve 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":118,"sourceCode":"        children: [],\n        exports: {},\n        filename,\n        id: filename,\n        isPreloading: false,\n        loaded: false,\n        path: path.dirname(filename),\n      },\n      undefined,\n    );\n  }\n\n  private resolve(\n    from: string,\n    moduleName: string | undefined,\n    options: ResolveOptions = {},\n  ): string {\n    if (moduleName == null) {\n      throw new Error(\n        'The first argument to require.resolve must be a string. Received null or undefined.',\n      );\n    }\n\n    if (path.isAbsolute(moduleName)) {\n      const module = this.resolution.resolveCjsFromDirIfExists(\n        moduleName,\n        moduleName,\n        [],\n      );\n      if (module) {\n        return module;\n      }\n    } else if (options.paths) {\n      const module = this.resolution.resolveCjsStub(from, moduleName);\n\n      if (module) {\n        return module;","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-runtime/src/internals/cjsRequire.ts#L100-L136","documentation":"Thrown by `RequireBuilder.resolve` (cjsRequire.ts:112-121) when `require.resolve` is called with `null` or `undefined`. The runtime guards `moduleName == null` before doing any path work because every downstream step assumes a string.","triggerScenarios":"`require.resolve(null)`, `require.resolve(undefined)`, or `require.resolve(someVar)` where `someVar` was never assigned. Dynamic builds of the module name that occasionally yield `null`.","commonSituations":"Config-driven code where a path comes from an optional env var that wasn't set. Refactors that pass `moduleName` through optional chaining.","solutions":["Ensure the argument is a string before calling: `if (typeof name === 'string') require.resolve(name)`.","Default the variable: `const name = process.env.MODULE ?? 'fallback'; require.resolve(name);`.","Add a precondition check at the boundary that produced the null/undefined."],"exampleFix":"// before\nrequire.resolve(process.env.TARGET_MODULE); // throws if unset\n\n// after\nconst target = process.env.TARGET_MODULE;\nif (typeof target === 'string') require.resolve(target);\nelse throw new Error('TARGET_MODULE must be set');","handlingStrategy":"type-guard","validationCode":"if (typeof name !== 'string' || name.length === 0) {\n  throw new Error('require.resolve needs a non-empty string');\n}\nrequire.resolve(name);","typeGuard":"function isNonEmptyString(v: unknown): v is string {\n  return typeof v === 'string' && v.length > 0;\n}","tryCatchPattern":null,"preventionTips":["Type the variable as `string` (not `string | undefined`) at the boundary that produces it.","Default optional env-sourced names to a known string or fail fast before require.resolve."],"tags":["require-resolve","validation","null-check"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}