jestjs/jest · error · Error
The first argument to require.resolve must be a string. Rece
Error message
The first argument to require.resolve must be a string. Received null or undefined.
What it means
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.
Source
Thrown at packages/jest-runtime/src/internals/cjsRequire.ts:118
children: [],
exports: {},
filename,
id: filename,
isPreloading: false,
loaded: false,
path: path.dirname(filename),
},
undefined,
);
}
private resolve(
from: string,
moduleName: string | undefined,
options: ResolveOptions = {},
): string {
if (moduleName == null) {
throw new Error(
'The first argument to require.resolve must be a string. Received null or undefined.',
);
}
if (path.isAbsolute(moduleName)) {
const module = this.resolution.resolveCjsFromDirIfExists(
moduleName,
moduleName,
[],
);
if (module) {
return module;
}
} else if (options.paths) {
const module = this.resolution.resolveCjsStub(from, moduleName);
if (module) {
return module;View on GitHub (pinned to f49721c78e)
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.
Example fix
// before
require.resolve(process.env.TARGET_MODULE); // throws if unset
// after
const target = process.env.TARGET_MODULE;
if (typeof target === 'string') require.resolve(target);
else throw new Error('TARGET_MODULE must be set'); Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof name !== 'string' || name.length === 0) {
throw new Error('require.resolve needs a non-empty string');
}
require.resolve(name); Type guard
function isNonEmptyString(v: unknown): v is string {
return typeof v === 'string' && v.length > 0;
} Prevention
- 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.
When it happens
Trigger: `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`.
Common situations: Config-driven code where a path comes from an optional env var that wasn't set. Refactors that pass `moduleName` through optional chaining.
Related errors
- The first argument to require.resolve.paths must be a string
- The first argument to require.resolve.paths must not be the
- @jest/diff-sequences: ${name} typeof ${typeof arg} is not a
- @jest/diff-sequences: ${name} value ${arg} is not a safe int
- @jest/diff-sequences: ${name} value ${arg} is a negative int
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/846158abff1730f3.json.
Report an issue: GitHub.