{"record":{"id":"711154d2f6b26b04","repo":"SeleniumHQ/selenium","slug":"port-must-be-a-number-received-port","errorCode":null,"errorMessage":"Port must be a number. Received:'${port}'","messagePattern":"Port must be a number\\. Received:'(.+?)'","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"javascript/selenium-webdriver/bidi/urlPattern.js","lineNumber":58,"sourceCode":"   * @returns {UrlPattern} - Returns the updated instance of the URL pattern for chaining.\n   */\n  hostname(hostname) {\n    this.#map.set('hostname', hostname)\n    return this\n  }\n\n  /**\n   * Sets the port for the URL pattern.\n   *\n   * @param {number} port - The port number to set.\n   * @returns {UrlPattern} - Returns the updated instance of the URL pattern for chaining.\n   * @throws {Error} - Throws an error if the port is not a number.\n   */\n  port(port) {\n    if (typeof port === 'number') {\n      this.#map.set('port', port.toString())\n    } else {\n      throw new Error(`Port must be a number. Received:'${port}'`)\n    }\n    return this\n  }\n\n  /**\n   * Sets the pathname for the URL pattern.\n   *\n   * @param {string} pathname - The pathname to set.\n   * @returns {UrlPattern} - Returns the updated instance of the URL pattern for chaining.\n   */\n  pathname(pathname) {\n    this.#map.set('pathname', pathname)\n    return this\n  }\n\n  /**\n   * Sets the search parameter in the URL pattern.\n   *","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/javascript/selenium-webdriver/bidi/urlPattern.js#L40-L76","documentation":"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.","triggerScenarios":"Calling urlPattern.port('8080') with a string. Passing a port read from process.env (always a string). Passing undefined, null, or a boolean.","commonSituations":"Reading port from environment variables or config files which yield strings; passing a port extracted from a URL string without conversion.","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."],"exampleFix":"// before\npattern.port(process.env.PORT)\n// after\npattern.port(Number(process.env.PORT))","handlingStrategy":"validation","validationCode":"if (typeof port !== 'number' || !Number.isFinite(port)) {\n  throw new TypeError(`port must be a finite number, got ${typeof port}`)\n}\npattern.port(port)","typeGuard":"/**\n * @param {*} p\n * @returns {p is number}\n */\nfunction isValidPort(p) {\n  return typeof p === 'number' && Number.isFinite(p) && p > 0 && p < 65536\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["bidi","validation","url-pattern","type","network"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}