denoland/deno · error · NodeTypeError

ERR_INVALID_ARG_TYPE

ERR_INVALID_ARG_TYPE

Error message

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

What it means

A Writable constructed without objectMode accepts only string, Buffer, or ArrayBuffer-view (TypedArray/DataView) chunks. The type dispatch at the end of the non-object-mode branch in _write() throws ERR_INVALID_ARG_TYPE for every other value: numbers, plain objects, arrays, undefined, booleans.

Source

Thrown at ext/node/polyfills/internal/streams/writable.js:570

    } else if (encoding !== "buffer" && !Buffer.isEncoding(encoding)) {
      throw new ERR_UNKNOWN_ENCODING(encoding);
    }

    if (typeof chunk === "string") {
      if (encoding === "buffer") {
        throw new ERR_UNKNOWN_ENCODING(encoding);
      }
      if ((state[kState] & kDecodeStrings) !== 0) {
        chunk = Buffer.from(chunk, encoding);
        encoding = "buffer";
      }
    } else if (chunk instanceof Buffer) {
      encoding = "buffer";
    } else if (Stream._isArrayBufferView(chunk)) {
      chunk = Stream._uint8ArrayToBuffer(chunk);
      encoding = "buffer";
    } else {
      throw new ERR_INVALID_ARG_TYPE(
        "chunk",
        ["string", "Buffer", "TypedArray", "DataView"],
        chunk,
      );
    }
  }

  let err;
  if ((state[kState] & kEnding) !== 0) {
    err = new ERR_STREAM_WRITE_AFTER_END();
  } else if ((state[kState] & kDestroyed) !== 0) {
    err = new ERR_STREAM_DESTROYED("write");
  }

  if (err) {
    process.nextTick(cb, err);
    errorOrDestroy(stream, err, true);
    return err;

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Construct the stream with objectMode when passing objects: new Writable({objectMode: true, write(chunk, enc, cb){...}})
  2. Serialize structured data at the boundary: ws.write(JSON.stringify(obj))
  3. Convert to bytes explicitly: ws.write(Buffer.from(String(n))) or Buffer.from(JSON.stringify(obj))

Example fix

// before
const ws = new Writable({ write(c, e, cb) { cb(); } });
ws.write({ id: 1 }); // throws ERR_INVALID_ARG_TYPE

// after
const ws = new Writable({ objectMode: true, write(c, e, cb) { cb(); } });
ws.write({ id: 1 }); // ok
Defensive patterns

Strategy: type-guard

Validate before calling

const ok = typeof chunk === 'string' || Buffer.isBuffer(chunk) || ArrayBuffer.isView(chunk);
if (!ok && !ws.writableObjectMode) chunk = Buffer.from(JSON.stringify(chunk));
ws.write(chunk);

Type guard

function isByteOrStringChunk(c) {
  return typeof c === 'string' || Buffer.isBuffer(c) || ArrayBuffer.isView(c);
}

Try / catch

try {
  ws.write(chunk);
} catch (err) {
  if (err.code === 'ERR_INVALID_ARG_TYPE') ws.write(JSON.stringify(chunk));
  else throw err;
}

Prevention

When it happens

Trigger: ws.write(42), ws.write({json: 'obj'}), ws.write([1,2,3]) on a non-objectMode stream; piping an object-producing source (JSON parser, DB cursor) into a plain byte Writable.

Common situations: Switching a pipeline from strings to structured records without {objectMode: true}; writing JSON.parse output or numeric counters directly; reusing a file-oriented stream for structured events.

Understand the failure class

Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/1dbfa0b4e1ca55d3. Report an issue: GitHub.