SeleniumHQ/selenium · error · Error

Port must be a number. Received:'${port}'

Error message

Port must be a number. Received:'${port}'

What it means

Thrown by UrlPattern.port() (BiDi network module) when the argument's typeof is not 'number'. The method converts the number to a string for storage but enforces a numeric input as a type contract. Non-numeric ports (strings, undefined, objects) are rejected.

Source

Thrown at javascript/selenium-webdriver/bidi/urlPattern.js:58

   * @returns {UrlPattern} - Returns the updated instance of the URL pattern for chaining.
   */
  hostname(hostname) {
    this.#map.set('hostname', hostname)
    return this
  }

  /**
   * Sets the port for the URL pattern.
   *
   * @param {number} port - The port number to set.
   * @returns {UrlPattern} - Returns the updated instance of the URL pattern for chaining.
   * @throws {Error} - Throws an error if the port is not a number.
   */
  port(port) {
    if (typeof port === 'number') {
      this.#map.set('port', port.toString())
    } else {
      throw new Error(`Port must be a number. Received:'${port}'`)
    }
    return this
  }

  /**
   * Sets the pathname for the URL pattern.
   *
   * @param {string} pathname - The pathname to set.
   * @returns {UrlPattern} - Returns the updated instance of the URL pattern for chaining.
   */
  pathname(pathname) {
    this.#map.set('pathname', pathname)
    return this
  }

  /**
   * Sets the search parameter in the URL pattern.
   *

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Convert the value to a number before calling: urlPattern.port(Number(port)).
  2. Parse with parseInt: urlPattern.port(parseInt(port, 10)).
  3. Guard with a typeof check and skip the call if not numeric.

Example fix

// before
pattern.port(process.env.PORT)
// after
pattern.port(Number(process.env.PORT))
Defensive patterns

Strategy: validation

Validate before calling

if (typeof port !== 'number' || !Number.isFinite(port)) {
  throw new TypeError(`port must be a finite number, got ${typeof port}`)
}
pattern.port(port)

Type guard

/**
 * @param {*} p
 * @returns {p is number}
 */
function isValidPort(p) {
  return typeof p === 'number' && Number.isFinite(p) && p > 0 && p < 65536
}

Prevention

When it happens

Trigger: Calling urlPattern.port('8080') with a string. Passing a port read from process.env (always a string). Passing undefined, null, or a boolean.

Common situations: Reading port from environment variables or config files which yield strings; passing a port extracted from a URL string without conversion.

Related errors


AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14). Data as JSON: /api/errors/711154d2f6b26b04. Report an issue: GitHub.