jestjs/jest · error · Error

The first argument to require.resolve.paths must not be the

Error message

The first argument to require.resolve.paths must not be the empty string.

What it means

Thrown by `RequireBuilder.resolvePaths` (cjsRequire.ts:182-186) when `require.resolve.paths('')` is called. An empty string can't be a module name and would produce nonsense search paths, so the runtime rejects it explicitly after the null check.

Source

Thrown at packages/jest-runtime/src/internals/cjsRequire.ts:183

      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];
  }
}

export interface CoreModuleProviderOptions {
  resolution: Resolution;

View on GitHub (pinned to f49721c78e)

Solutions

  1. Validate `name.length > 0` before calling.
  2. Fix the upstream code that produced an empty module name.
  3. Treat empty as a no-op rather than passing it through.

Example fix

// before
require.resolve.paths(name.trim()); // throws if name is whitespace-only

// after
const trimmed = name.trim();
if (trimmed.length > 0) require.resolve.paths(trimmed);
Defensive patterns

Strategy: validation

Validate before calling

if (typeof name !== 'string' || name.trim().length === 0) {
  throw new Error('require.resolve.paths needs a non-empty string');
}
require.resolve.paths(name);

Type guard

function isNonEmptyString(v: unknown): v is string {
  return typeof v === 'string' && v.trim().length > 0;
}

Prevention

When it happens

Trigger: `require.resolve.paths('')`, or a variable that was trimmed/sanitized down to empty. Programmatically built names that collapse to empty under some inputs.

Common situations: Parsing logic that strips prefixes and yields empty for certain inputs. Copy-paste from a config that had a placeholder.

Related errors


AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03). Data as JSON: /data/errors/802f621e1300a5a2.json. Report an issue: GitHub.