nodejs/node · error · InvalidArgumentError

UND_ERR_INVALID_ARG

UND_ERR_INVALID_ARG

Error message

path must be a string

What it means

Thrown by the Request constructor when path is not a string. path is destructured from the second argument and must be a string before any further validation; anything else (number, object, null, undefined, Buffer) fails immediately.

Source

Thrown at deps/undici/src/lib/core/request.js:117

    path,
    method,
    body,
    headers,
    query,
    idempotent,
    blocking,
    upgrade,
    headersTimeout,
    bodyTimeout,
    reset,
    expectContinue,
    servername,
    throwOnError,
    maxRedirections,
    typeOfService
  }, handler) {
    if (typeof path !== 'string') {
      throw new InvalidArgumentError('path must be a string')
    } else if (
      path[0] !== '/' &&
      !(path.startsWith('http://') || path.startsWith('https://')) &&
      method !== 'CONNECT'
    ) {
      throw new InvalidArgumentError('path must be an absolute URL or start with a slash')
    } else if (invalidPathRegex.test(path)) {
      throw new InvalidArgumentError('invalid request path')
    }

    if (typeof method !== 'string') {
      throw new InvalidArgumentError('method must be a string')
    } else if (normalizedMethodRecords[method] === undefined && !isValidHTTPToken(method)) {
      throw new InvalidArgumentError('invalid request method')
    }

    if (upgrade && typeof upgrade !== 'string') {
      throw new InvalidArgumentError('upgrade must be a string')

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Always pass a string path: new Request(origin, { path: '/users?id=1', method: 'GET' }).
  2. If you have a URL object, pass url.pathname + url.search as path.
  3. If you have a full URL, pass it as path (http://.../https://... are allowed).
  4. Default path to '/' when your caller may omit it.

Example fix

// before
new Request(origin, { path: new URL('/x', origin), method: 'GET' }, handler)
// after
const u = new URL('/x', origin)
new Request(origin, { path: u.pathname + u.search, method: 'GET' }, handler)
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof path !== 'string' || path.length === 0) {
  throw new TypeError('path must be a non-empty string')
}

Type guard

function isPathString(p) {
  return typeof p === 'string' && p.length > 0
}

Prevention

When it happens

Trigger: Constructing a Request with path: undefined (most common — the field was omitted), path: 42, path: new URL(...), or path: someObject. Also when a URL object is passed where undici expects the pathname string.

Common situations: Building a request from a URL object and passing the whole object as path instead of url.pathname + url.search; spreading an options object that omits path; refactoring that renamed the field.

Related errors


AI-assisted analysis of nodejs/node@1b2de5e052 (2026-08-13). Data as JSON: /api/errors/fd18bce784a003d4. Report an issue: GitHub.