{"record":{"id":"1dbfa0b4e1ca55d3","repo":"denoland/deno","slug":"err-invalid-arg-type-1dbfa0","errorCode":"ERR_INVALID_ARG_TYPE","errorMessage":"The \"chunk\" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received ${chunk}","messagePattern":"The \"chunk\" argument must be of type string or an instance of Buffer, TypedArray, or DataView\\. Received (.+?)","errorType":"exception","errorClass":"NodeTypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/internal/streams/writable.js","lineNumber":570,"sourceCode":"    } else if (encoding !== \"buffer\" && !Buffer.isEncoding(encoding)) {\n      throw new ERR_UNKNOWN_ENCODING(encoding);\n    }\n\n    if (typeof chunk === \"string\") {\n      if (encoding === \"buffer\") {\n        throw new ERR_UNKNOWN_ENCODING(encoding);\n      }\n      if ((state[kState] & kDecodeStrings) !== 0) {\n        chunk = Buffer.from(chunk, encoding);\n        encoding = \"buffer\";\n      }\n    } else if (chunk instanceof Buffer) {\n      encoding = \"buffer\";\n    } else if (Stream._isArrayBufferView(chunk)) {\n      chunk = Stream._uint8ArrayToBuffer(chunk);\n      encoding = \"buffer\";\n    } else {\n      throw new ERR_INVALID_ARG_TYPE(\n        \"chunk\",\n        [\"string\", \"Buffer\", \"TypedArray\", \"DataView\"],\n        chunk,\n      );\n    }\n  }\n\n  let err;\n  if ((state[kState] & kEnding) !== 0) {\n    err = new ERR_STREAM_WRITE_AFTER_END();\n  } else if ((state[kState] & kDestroyed) !== 0) {\n    err = new ERR_STREAM_DESTROYED(\"write\");\n  }\n\n  if (err) {\n    process.nextTick(cb, err);\n    errorOrDestroy(stream, err, true);\n    return err;","sourceCodeStart":552,"sourceCodeEnd":588,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/internal/streams/writable.js#L552-L588","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Construct the stream with objectMode when passing objects: new Writable({objectMode: true, write(chunk, enc, cb){...}})","Serialize structured data at the boundary: ws.write(JSON.stringify(obj))","Convert to bytes explicitly: ws.write(Buffer.from(String(n))) or Buffer.from(JSON.stringify(obj))"],"exampleFix":"// before\nconst ws = new Writable({ write(c, e, cb) { cb(); } });\nws.write({ id: 1 }); // throws ERR_INVALID_ARG_TYPE\n\n// after\nconst ws = new Writable({ objectMode: true, write(c, e, cb) { cb(); } });\nws.write({ id: 1 }); // ok","handlingStrategy":"type-guard","validationCode":"const ok = typeof chunk === 'string' || Buffer.isBuffer(chunk) || ArrayBuffer.isView(chunk);\nif (!ok && !ws.writableObjectMode) chunk = Buffer.from(JSON.stringify(chunk));\nws.write(chunk);","typeGuard":"function isByteOrStringChunk(c) {\n  return typeof c === 'string' || Buffer.isBuffer(c) || ArrayBuffer.isView(c);\n}","tryCatchPattern":"try {\n  ws.write(chunk);\n} catch (err) {\n  if (err.code === 'ERR_INVALID_ARG_TYPE') ws.write(JSON.stringify(chunk));\n  else throw err;\n}","preventionTips":["Decide up front whether the stream is objectMode","Serialize structured data at the pipeline boundary","Check stream.writableObjectMode before writing records"],"tags":["streams","writable","type-validation","node-compat"],"backgroundTag":"invalid-argument-type","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}