Data cannot be sent on this socket
Production Risk
Low — manage socket lifecycle explicitly; track close events before sending.
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.
- 1Sending data after the socket has been closed
- 2Operating system rejected the send operation due to resource limits
- 3Socket is half-closed and the write side is no longer available
Triggered when the dgram or net socket send operation fails at the OS level.
const dgram = require('dgram');
const s = dgram.createSocket('udp4');
s.close();
s.send('data', 41234, 'localhost'); // throws — socket closedexpected 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
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.
const dgram = require('dgram');
const s = dgram.createSocket('udp4');
s.close();
s.send('data', 41234, 'localhost'); // throws — socket closed // this triggers ERR_SOCKET_CANNOT_SENDtry {
// 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
}
}// Validate inputs before calling the operation
function safe_err_socket_cannot_send(...args) {
// validate args here
return performOperation(...args)
}✕ Send data on sockets after calling close()
A closed socket has no OS file descriptor; sends cannot be delivered.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev