denoland/deno · error · TypeError

ERR_INVALID_ARG_TYPE

ERR_INVALID_ARG_TYPE

Error message

The "buffer" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received ${actual}

What it means

dgram.Socket.send validates the payload before dispatch: when the msg argument is not an array, it must be a string or an ArrayBufferView (Buffer, TypedArray, DataView). Anything else — number, plain object, null, Blob — throws this ERR_INVALID_ARG_TYPE immediately.

Source

Thrown at ext/node/polyfills/dgram.ts:968

        if (typeof port === "function") {
          callback = port;
          port = null;
        }
      } else {
        callback = offset;
      }

      if (port || address) {
        throw new ERR_SOCKET_DGRAM_IS_CONNECTED();
      }
    }

    if (!ArrayIsArray(buffer)) {
      if (typeof buffer === "string") {
        list = [Buffer.from(buffer)];
      } else if (!isArrayBufferView(buffer)) {
        throw new ERR_INVALID_ARG_TYPE(
          "buffer",
          ["Buffer", "TypedArray", "DataView", "string"],
          buffer,
        );
      } else {
        list = [buffer as MessageType];
      }
    } else if (!(list = fixBufferList(buffer))) {
      throw new ERR_INVALID_ARG_TYPE(
        "buffer list arguments",
        ["Buffer", "TypedArray", "DataView", "string"],
        buffer,
      );
    }

    if (!connected) {
      port = validatePort(port, "Port", false);
    }

View on GitHub (pinned to 89f33cbef2)

Solutions

  1. Serialize the payload: socket.send(Buffer.from(JSON.stringify(obj)), port, host)
  2. Use Buffer.from(String(value)) for scalars, or str2ab/TextEncoder for strings you want as bytes
  3. If you meant to send several chunks, pass an array of Buffers (each element must itself be valid)

Example fix

// before
sock.send({ op: 'PING', seq: 7 }, 41234, '192.168.1.10'); // throws

// after
sock.send(Buffer.from(JSON.stringify({ op: 'PING', seq: 7 })), 41234, '192.168.1.10');
Defensive patterns

Strategy: type-guard

Validate before calling

function toMessage(v) {
  if (typeof v === 'string') return Buffer.from(v);
  if (ArrayBuffer.isView(v)) return v;
  throw new TypeError(`unsupported dgram payload: ${typeof v}`);
}
sock.send(toMessage(payload), port, host);

Type guard

function isDgramPayload(v) {
  return typeof v === 'string' || ArrayBuffer.isView(v);
}

Prevention

When it happens

Trigger: socket.send(42, port, host); socket.send({ type: 'ping' }, port, host); socket.send(null, cb); socket.send(new Blob(['x']), port) — any non-array, non-string, non-view single payload.

Common situations: Sending a JSON object without stringify/Buffer.from; passing a numeric id or computed value instead of its serialized bytes; passing a Blob where Node's dgram expects a Buffer.

Related errors


AI-assisted analysis of denoland/deno@89f33cbef2 (2026-08-16). Data as JSON: /api/errors/c24b2eb28233cee3. Report an issue: GitHub.