denoland/deno · error · TypeError
ERR_SOCKET_BAD_TYPE
ERR_SOCKET_BAD_TYPE
Error message
Bad socket type specified. Valid types are: udp4, udp6
What it means
dgram.createSocket() builds its native handle through newHandle(type): only exact lowercase 'udp4' and 'udp6' are accepted; every other value falls through to ERR_SOCKET_BAD_TYPE. Unlike Node, Deno's polyfill does not implement 'unix_dgram', so that otherwise-valid Node type also throws here.
Source
Thrown at ext/node/polyfills/internal/dgram.ts:117
const handle = new UDP();
handle.lookup = FunctionPrototypeBind(lookup4, handle, lookup);
return handle;
}
if (type === "udp6") {
const handle = new UDP();
handle.lookup = FunctionPrototypeBind(lookup6, handle, lookup);
handle.bind = handle.bind6;
handle.connect = handle.connect6;
handle.send = handle.send6;
return handle;
}
throw new ERR_SOCKET_BAD_TYPE();
}
function _createSocketHandle(
address: string,
port: number,
addressType: SocketType,
fd: number,
flags: number,
) {
const handle = newHandle(addressType);
let err;
if (isInt32(fd) && fd > 0) {
const type = guessHandleType(fd);
if (type !== "UDP") {
err = MapPrototypeGet(codeMap, "EINVAL")!;
} else {View on GitHub (pinned to 9ad36f7a2c)
Solutions
- Use exactly 'udp4' or 'udp6'.
- Replace unix datagram IPC with Deno.connect({ transport: 'unix' }) streams or a loopback UDP socket.
- Normalize and whitelist config-driven types before createSocket (lowercase, trim, allowlist).
Example fix
// before const sock = dgram.createSocket(config.sockType); // 'unix_dgram' // after const sock = dgram.createSocket(config.sockType === 'udp6' ? 'udp6' : 'udp4');
Defensive patterns
Strategy: validation
Validate before calling
const SOCKET_TYPES = new Set(['udp4', 'udp6']);
const type = String(config.socketType ?? '').toLowerCase().trim();
if (!SOCKET_TYPES.has(type)) {
throw new RangeError('socket type must be udp4 or udp6, got ' + config.socketType);
}
const sock = dgram.createSocket(type); Type guard
const isUdpType = (t) => t === 'udp4' || t === 'udp6';
Try / catch
try {
sock = dgram.createSocket(type);
} catch (err) {
if (err?.code === 'ERR_SOCKET_BAD_TYPE') {
sock = dgram.createSocket('udp4'); // safe default after logging the config error
} else {
throw err;
}
} Prevention
- Whitelist socket types from config; normalize case and trim before createSocket.
- Do not port Node unix_dgram code to Deno node:dgram - redesign to unix streams or loopback UDP.
- Fail fast on unknown socket types at startup, not mid-request.
When it happens
Trigger: dgram.createSocket('unix_dgram') (unix datagram socket, unsupported); createSocket('UDP4') or 'udp4 ' (case/whitespace mismatch); a type variable that is undefined or came from unvalidated config.
Common situations: Porting Node services that log to a local unix datagram socket (syslog-style) to Deno; config-driven socket types with casing mismatches; feature flags selecting socket families.
Related errors
- invalid IPv4 address
- ERR_MISSING_ARGS
- ERR_SOCKET_ALREADY_BOUND
- ERR_INVALID_FD_TYPE
- ERR_SOCKET_DGRAM_IS_CONNECTED
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/315f7e72d4be7570.
Report an issue: GitHub.