{"record":{"id":"7ae577f666b4e966","repo":"denoland/deno","slug":"err-stream-null-values-7ae577","errorCode":"ERR_STREAM_NULL_VALUES","errorMessage":"May not write null values to stream","messagePattern":"May not write null values to stream","errorType":"exception","errorClass":"NodeTypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/internal/streams/writable.js","lineNumber":544,"sourceCode":"\n    return object && object._writableState instanceof WritableState;\n  },\n});\n\n// Otherwise people can pipe Writable streams, which is just wrong.\nWritable.prototype.pipe = function () {\n  errorOrDestroy(this, new ERR_STREAM_CANNOT_PIPE());\n};\n\nfunction _write(stream, chunk, encoding, cb) {\n  const state = stream._writableState;\n\n  if (cb == null || typeof cb !== \"function\") {\n    cb = nop;\n  }\n\n  if (chunk === null) {\n    throw new ERR_STREAM_NULL_VALUES();\n  }\n\n  if ((state[kState] & kObjectMode) === 0) {\n    if (!encoding) {\n      encoding = (state[kState] & kDefaultUTF8Encoding) !== 0\n        ? \"utf8\"\n        : state.defaultEncoding;\n    } 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\";","sourceCodeStart":526,"sourceCodeEnd":562,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/internal/streams/writable.js#L526-L562","documentation":"Thrown when null is passed as a chunk to a Writable stream's write() path. The internal _write() helper rejects chunk === null before any object-mode handling, so null is illegal in every mode: null is reserved as the 'no chunk' signal for end(), not as data. This matches Node.js ERR_STREAM_NULL_VALUES behavior in Deno's Node-compat streams polyfill.","triggerScenarios":"stream.write(null), stream.write(null, cb), or a pipeline feeding nullable values (DB cursor rows, optional JSON fields) into a Node-style Writable from the ext/node polyfills.","commonSituations":"Streaming optional/nullable fields without filtering; map steps that can produce null; porting code that used null to mean 'nothing to write'; accidentally writing null when end() was intended.","solutions":["Filter null chunks before writing: if (chunk !== null) ws.write(chunk)","If null means 'done', call ws.end() with no arguments instead of writing null","Write '' or Buffer.alloc(0) when a zero-length write is intended"],"exampleFix":"// before\nws.write(row.optionalField); // optionalField can be null\n\n// after\nif (row.optionalField !== null) ws.write(row.optionalField);","handlingStrategy":"validation","validationCode":"const chunk = next();\nif (chunk === null) {\n  // skip, or treat as end: ws.end(); return;\n}\nws.write(chunk);","typeGuard":"function isWritableChunk(c) {\n  return c !== null && (typeof c === 'string' || Buffer.isBuffer(c) || ArrayBuffer.isView(c));\n}","tryCatchPattern":"ws.on('error', (err) => {\n  if (err.code === 'ERR_STREAM_NULL_VALUES') {\n    console.error('null chunk produced upstream — fix the source');\n  }\n});","preventionTips":["Treat null as a control signal (end), never as data","Filter nullable values where they are produced, not at the stream boundary","Test pipelines with nullable fixture rows"],"tags":["streams","writable","node-compat","deno"],"backgroundTag":"stream-invalid-write-chunk","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}