ERR_SOCKET_DGRAM_NOT_RUNNING
Node.jsERRORNotableDNSHIGH confidence

UDP socket is not running

Production Risk

Low — manage UDP socket lifecycle carefully in async code.

What this means

Thrown when an operation is attempted on a UDP (dgram) socket that has not been bound or has already been closed. Common operations like send() and addMembership() require the socket to be in a running state.

Why it happens
  1. 1Calling socket.send() before socket.bind() completes
  2. 2Using a socket after socket.close() has been called
  3. 3Race condition where send() is called before the bind callback fires
How to reproduce

Triggered when a dgram operation is attempted on a socket that is not running.

trigger — this will error
trigger — this will error
const dgram = require('dgram');
const socket = dgram.createSocket('udp4');
socket.close(); // socket is now stopped
socket.send('data', 41234, 'localhost'); // throws

expected output

Error [ERR_SOCKET_DGRAM_NOT_RUNNING]: Not running

Fix

Only send after binding and before closing

WHEN When managing UDP socket lifecycle

Only send after binding and before closing
const socket = dgram.createSocket('udp4');
socket.bind(() => {
  socket.send(Buffer.from('hello'), 41234, 'localhost', (err) => {
    if (err) console.error(err);
    socket.close();
  });
});

Why this works

Performing sends inside the bind callback ensures the socket is running before use.

Code examples
Triggerjs
const dgram = require('dgram');
const socket = dgram.createSocket('udp4');
socket.close(); // socket is now stopped
socket.send('data', 41234, 'localhost'); // throws  // this triggers ERR_SOCKET_DGRAM_NOT_RUNNING
Handle in try/catchjs
try {
  // operation that may throw ERR_SOCKET_DGRAM_NOT_RUNNING
  riskyOperation()
} catch (err) {
  if (err.code === 'ERR_SOCKET_DGRAM_NOT_RUNNING') {
    console.error('ERR_SOCKET_DGRAM_NOT_RUNNING:', err.message)
  } else {
    throw err
  }
}
Defensive pattern to avoid itjs
// Validate inputs before calling the operation
function safe_err_socket_dgram_not_running(...args) {
  // validate args here
  return performOperation(...args)
}
What not to do

Send data on a dgram socket after close()

A closed socket has no OS binding and cannot send or receive data.

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