nodejs/node · error · InvalidArgumentError

connect must be a function or an object

Error message

connect must be a function or an object

What it means

Thrown by the RoundRobinPool constructor when connect is present but neither a function nor an object. As with Pool, connect may be a custom connector function or a plain options object fed to buildConnector; any other type is rejected. Guard: connect != null && typeof connect !== 'function' && typeof connect !== 'object'.

Source

Thrown at deps/undici/src/lib/dispatcher/round-robin-pool.js:53

    tls,
    maxCachedSessions,
    socketPath,
    autoSelectFamily,
    autoSelectFamilyAttemptTimeout,
    allowH2,
    clientTtl,
    ...options
  } = {}) {
    if (connections != null && (!Number.isFinite(connections) || connections < 0)) {
      throw new InvalidArgumentError('invalid connections')
    }

    if (typeof factory !== 'function') {
      throw new InvalidArgumentError('factory must be a function.')
    }

    if (connect != null && typeof connect !== 'function' && typeof connect !== 'object') {
      throw new InvalidArgumentError('connect must be a function or an object')
    }

    if (typeof connect !== 'function') {
      connect = buildConnector({
        ...tls,
        maxCachedSessions,
        allowH2,
        socketPath,
        timeout: connectTimeout,
        ...(typeof autoSelectFamily === 'boolean' ? { autoSelectFamily, autoSelectFamilyAttemptTimeout } : undefined),
        ...connect
      })
    }

    super()

    this[kConnections] = connections || null
    this[kUrl] = util.parseOrigin(origin)

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Pass connect as a function (custom connector) or a plain object (connector/TLS options).
  2. Route host/port/socket config to the correct keys (socketPath, tls, etc.).
  3. Ensure deserialized config yields an object for connect.
  4. Omit connect to let RoundRobinPool build the default connector.

Example fix

// before
new RoundRobinPool(url, { connect: 'upstream.local:443' })
// after
new RoundRobinPool(url, { connect: { rejectUnauthorized: true } })
Defensive patterns

Strategy: type-guard

Validate before calling

if (cfg.connect != null && typeof cfg.connect !== 'function' && typeof cfg.connect !== 'object') {
  throw new TypeError('connect must be a function or object')
}
new RoundRobinPool(url, { connect: cfg.connect })

Type guard

function isValidConnect(v) {
  return v == null || typeof v === 'function' || (typeof v === 'object' && !Array.isArray(v))
}

Prevention

When it happens

Trigger: Passing connect as a string URL, a number, an array, or a non-plain object. The check fires only when connect is non-null, so omitting it is safe.

Common situations: Mistaking connect for a host:port string; passing a TLS bag under the wrong key; JSON config decoding connect to a string; refactoring that wrapped the connector.

Related errors


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