jestjs/jest · error · Error
The first argument to require.resolve.paths must be a string
Error message
The first argument to require.resolve.paths must be a string. Received null or undefined.
What it means
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.
Source
Thrown at packages/jest-runtime/src/internals/cjsRequire.ts:178
try {
return this.resolution.resolveCjs(from, moduleName);
} catch (error) {
const module = this.resolution.getCjsMockModule(from, moduleName);
if (module) {
return module;
}
throw error;
}
}
private resolvePaths(
from: string,
moduleName: string | undefined,
): Array<string> | null {
const fromDir = path.resolve(from, '..');
if (moduleName == null) {
throw new Error(
'The first argument to require.resolve.paths must be a string. Received null or undefined.',
);
}
if (moduleName.length === 0) {
throw new Error(
'The first argument to require.resolve.paths must not be the empty string.',
);
}
if (moduleName[0] === '.') {
return [fromDir];
}
if (this.resolution.isCoreModule(moduleName)) {
return null;
}
const modulePaths = this.resolution.getModulePaths(fromDir);
const globalPaths = this.resolution.getGlobalPaths(moduleName);
return [...modulePaths, ...globalPaths];View on GitHub (pinned to f49721c78e)
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.
Example fix
// before
const searchPaths = require.resolve.paths(maybeModule); // throws when undefined
// after
const searchPaths =
typeof maybeModule === 'string'
? require.resolve.paths(maybeModule)
: null; Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof name !== 'string') {
throw new Error('require.resolve.paths needs a string');
}
require.resolve.paths(name); Type guard
function isString(v: unknown): v is string {
return typeof v === 'string';
} Prevention
- Always type introspection inputs as string at the source.
- Wrap require.resolve.paths calls behind a typed helper.
When it happens
Trigger: `require.resolve.paths(null)` or `require.resolve.paths(someOptional)` where `someOptional` is undefined.
Common situations: Tooling that introspects Node's resolution paths using a variable that came from optional config or an env var.
Related errors
- The first argument to require.resolve must be a string. Rece
- 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/04a4a50f5d0f7b49.json.
Report an issue: GitHub.