ERR_SOCKET_CANNOT_SEND
Node.jsERRORNotableNetworkHIGH confidence

Data cannot be sent on this socket

Production Risk

Low — manage socket lifecycle explicitly; track close events before sending.

What this means

Thrown when socket.send() fails because the socket is in a state that does not allow sending — typically because it has been closed or destroyed, or because the underlying OS send buffer is unavailable.

Why it happens
  1. 1Sending data after the socket has been closed
  2. 2Operating system rejected the send operation due to resource limits
  3. 3Socket is half-closed and the write side is no longer available
How to reproduce

Triggered when the dgram or net socket send operation fails at the OS level.

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

expected output

Error [ERR_SOCKET_CANNOT_SEND]: Failed to send

Fix

Ensure the socket is open before sending

WHEN When send may be called after close

Ensure the socket is open before sending
let closed = false;
s.on('close', () => { closed = true; });

function safeSend(data, port, host) {
  if (!closed) s.send(data, port, host);
}

Why this works

Tracking the closed state prevents send calls on a closed socket.

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

Send data on sockets after calling close()

A closed socket has no OS file descriptor; sends cannot be delivered.

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