Invalid socket type specified
Production Risk
Low — caught immediately at call site.
Thrown when an invalid socket type is passed to dgram.createSocket(). The only valid types are "udp4" (IPv4 UDP) and "udp6" (IPv6 UDP). Any other string causes this error.
- 1Passing "tcp" or "udp" (without version) to dgram.createSocket()
- 2Typo in the socket type string
- 3Passing the type as an options object without the type property
Triggered when dgram.createSocket() validates the type argument.
const dgram = require('dgram');
dgram.createSocket('udp'); // invalid — must be 'udp4' or 'udp6'expected output
TypeError [ERR_SOCKET_BAD_TYPE]: Bad socket type specified. Valid types are: udp4, udp6
Fix
Use "udp4" or "udp6" as the socket type
WHEN Always — these are the only valid dgram socket types
const dgram = require('dgram');
const socket4 = dgram.createSocket('udp4'); // IPv4 UDP
const socket6 = dgram.createSocket('udp6'); // IPv6 UDPWhy this works
Specifying the exact type string satisfies the validation in dgram.createSocket().
const dgram = require('dgram');
dgram.createSocket('udp'); // invalid — must be 'udp4' or 'udp6' // this triggers ERR_SOCKET_BAD_TYPEtry {
// operation that may throw ERR_SOCKET_BAD_TYPE
riskyOperation()
} catch (err) {
if (err.code === 'ERR_SOCKET_BAD_TYPE') {
console.error('ERR_SOCKET_BAD_TYPE:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_socket_bad_type(...args) {
// validate args here
return performOperation(...args)
}✕ Pass "tcp", "udp", "ip4", or other strings to createSocket()
Only "udp4" and "udp6" are valid socket types for the dgram module.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev