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
- Convert the value to a number before calling: urlPattern.port(Number(port)).
- Parse with parseInt: urlPattern.port(parseInt(port, 10)).
- 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
- Convert env/config port values with Number() before passing.
- Always validate typeof port === 'number' before calling UrlPattern.port().
- Remember that process.env values are always strings.
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
- Params must be an instance of CookieFilter. Received:'${cook
- {self._label.capitalize()} '{handler_id}' not found
- Extra header '{name}' not found
- Pattern must be an instance of UrlPattern. Received: '${patt
- Pattern must be an instance of UrlPattern. Received:'${patte
AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14).
Data as JSON: /api/errors/711154d2f6b26b04.
Report an issue: GitHub.