denoland/deno · error · TypeError
Invalid port
Error message
Invalid port: '${maybePort}' What it means
Thrown by Deno's port validator (validatePort in ext/net/01_net.js) when the port is a string that cannot be converted to a number: Number(maybePort) is NaN while the input itself is not NaN. It is a TypeError raised before any socket is created. Every port passed to Deno.connect, Deno.listen, Deno.connectTls, and Deno.listenDatagram goes through this check.
Solutions
- Pass the port as a number: Deno.connect({ hostname, port: 5432 })
- Coerce values from env/config at the boundary: const port = Number(raw); if (!Number.isInteger(port)) fail fast with a clear config error
- If the value comes from a URL, extract it with new URL(str).port and Number() it before calling
Example fix
// before
Deno.connect({ port: Deno.env.get('PORT') }); // PORT='auto' -> Invalid port: 'auto'
// after
const raw = Deno.env.get('PORT') ?? '8080';
const port = Number(raw);
if (!Number.isInteger(port) || port < 1 || port > 65535) {
throw new Error(`Invalid PORT value: ${raw}`);
}
Deno.connect({ port }); Defensive patterns
Strategy: validation
Validate before calling
function parsePort(raw: unknown): number {
if (typeof raw !== 'number' && typeof raw !== 'string') throw new TypeError('port must be number or string');
if (raw === '') throw new TypeError('port must not be empty');
const n = Number(raw);
if (!Number.isInteger(n)) throw new TypeError(`Invalid port: ${String(raw)}`);
return n;
}
// const port = parsePort(Deno.env.get('PORT')); Deno.connect({ port }); Type guard
function isValidPortSpec(p: unknown): p is number | string {
if (p === '' || p === null || p === undefined) return false;
return Number.isInteger(Number(p));
} Try / catch
try {
await Deno.connect({ port });
} catch (e) {
if (e instanceof TypeError && e.message.startsWith('Invalid port')) {
throw new Error(`Bad port in configuration: ${JSON.stringify(portArg)}`);
}
throw e;
} Prevention
- Coerce env/config port values with Number() once at the config boundary and store only numbers afterwards
- Type config as { port: number } so TypeScript rejects strings at compile time
- Log the raw port value whenever validation fails so misconfigured keys are obvious
When it happens
Trigger: Deno.connect({ hostname: 'db', port: 'localhost' }) (hostname value placed in the port field); Deno.listen({ port: '8080a' }); Deno.listenDatagram({ transport: 'udp', port: 'auto' }) where a non-numeric env/config string is forwarded.
Common situations: Swapping hostname and port arguments; reading the port from an environment variable or config file that contains text like 'auto', 'default', or 'PORT'; passing a full URL string ('localhost:5432') instead of just the port component.
Related errors
- Invalid port
- Invalid port: ''
- Invalid port (out of range)
- The provided value " " is not a valid permission name
- Unsupported transport
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/65b0339961b5abc2.
Report an issue: GitHub.
Appendix: source
Thrown at ext/net/01_net.js:651
throw new TypeError(`Unsupported transport: '${transport}'`);
}
}
function validatePort(maybePort, isServer = false) {
// A missing port means "any available port" (port 0) for servers. Clients
// must always specify a port to connect to, so a missing port is left as-is
// and rejected below.
if (isServer && (maybePort === null || maybePort === undefined)) {
maybePort = 0;
}
if (typeof maybePort !== "number" && typeof maybePort !== "string") {
throw new TypeError(`Invalid port (expected number): ${maybePort}`);
}
if (maybePort === "") throw new TypeError("Invalid port: ''");
const port = Number(maybePort);
if (!NumberIsInteger(port)) {
if (NumberIsNaN(port) && !NumberIsNaN(maybePort)) {
throw new TypeError(`Invalid port: '${maybePort}'`);
} else {
throw new TypeError(`Invalid port: ${maybePort}`);
}
} else if (port < (isServer ? 0 : 1) || port > 65535) {
// Servers may bind to port 0 (OS-assigned), clients may not connect to it.
throw new RangeError(`Invalid port (out of range): ${maybePort}`);
}
return port;
}
function createListenDatagram(udpOpFn, unixOpFn) {
return function listenDatagram(args) {
switch (args.transport) {
case "udp": {
const port = validatePort(args.port, true);
const { 0: rid, 1: addr } = udpOpFn(
{
hostname: args.hostname ?? "0.0.0.0",View on GitHub (pinned to 9ad36f7a2c)