ERR_SOCKET_BAD_TYPE
Node.jsERRORNotableNetworkHIGH confidence

Invalid socket type specified

Production Risk

Low — caught immediately at call site.

What this means

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.

Why it happens
  1. 1Passing "tcp" or "udp" (without version) to dgram.createSocket()
  2. 2Typo in the socket type string
  3. 3Passing the type as an options object without the type property
How to reproduce

Triggered when dgram.createSocket() validates the type argument.

trigger — this will error
trigger — this will error
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

Use "udp4" or "udp6" as the socket type
const dgram = require('dgram');
const socket4 = dgram.createSocket('udp4'); // IPv4 UDP
const socket6 = dgram.createSocket('udp6'); // IPv6 UDP

Why this works

Specifying the exact type string satisfies the validation in dgram.createSocket().

Code examples
Triggerjs
const dgram = require('dgram');
dgram.createSocket('udp'); // invalid — must be 'udp4' or 'udp6'  // this triggers ERR_SOCKET_BAD_TYPE
Handle in try/catchjs
try {
  // 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
  }
}
Defensive pattern to avoid itjs
// Validate inputs before calling the operation
function safe_err_socket_bad_type(...args) {
  // validate args here
  return performOperation(...args)
}
What not to do

Pass "tcp", "udp", "ip4", or other strings to createSocket()

Only "udp4" and "udp6" are valid socket types for the dgram module.

Sources
Official documentation ↗

Node.js Error Codes Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Node.js errors