{"record":{"id":"4e77326aa7c8e8a3","repo":"denoland/deno","slug":"err-stream-null-values","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/_http_outgoing.ts","lineNumber":1357,"sourceCode":"    !msg._removedContLen &&\n    !msg.chunkedEncoding &&\n    !msg.hasHeader(\"transfer-encoding\")\n  );\n}\n\nfunction write_(\n  msg: any,\n  chunk: any,\n  encoding: string | null,\n  callback: any,\n  fromEnd: boolean,\n): boolean {\n  if (typeof callback !== \"function\") {\n    callback = nop;\n  }\n\n  if (chunk === null) {\n    throw new ERR_STREAM_NULL_VALUES();\n  } else if (typeof chunk !== \"string\" && !isUint8Array(chunk)) {\n    throw new ERR_INVALID_ARG_TYPE(\n      \"chunk\",\n      [\"string\", \"Buffer\", \"Uint8Array\"],\n      chunk,\n    );\n  }\n\n  let err;\n  if (msg.finished) {\n    err = new ERR_STREAM_WRITE_AFTER_END();\n  } else if (msg.destroyed) {\n    err = new ERR_STREAM_DESTROYED(\"write\");\n  }\n\n  if (err) {\n    if (!msg.destroyed) {\n      _onError(msg, err, callback);","sourceCodeStart":1339,"sourceCodeEnd":1375,"githubUrl":"https://github.com/denoland/deno/blob/89f33cbef296a2b287f323d42de54c871fa69c77/ext/node/polyfills/_http_outgoing.ts#L1339-L1375","documentation":"The internal write_() path behind write() and end() throws ERR_STREAM_NULL_VALUES when the chunk is exactly `null`. Older Node silently ignored null chunks; current Node (and this polyfill) reject them for strictness, because a null byte count is ambiguous.","triggerScenarios":"res.write(null), res.end(null), or req.end(null, cb); classic pattern `res.end(data || null)` where data is empty.","commonSituations":"Optional payloads forwarded with `||` defaulting to null; APIs ported from old Node where null was tolerated; passing a null from an empty option straight into end().","solutions":["End without a body: res.end()","Send an empty payload explicitly: res.end('') or res.end(Buffer.alloc(0))","Fix the default: `res.end(data ?? '')` instead of `data || null`"],"exampleFix":"// before\nres.end(data || null); // data is '' -> end(null) throws ERR_STREAM_NULL_VALUES\n\n// after\nres.end(data ?? '');","handlingStrategy":"validation","validationCode":"// never pass null as a chunk\nfunction safeEnd(res: http.ServerResponse, data?: string | Buffer | null) {\n  if (data === null || data === undefined) res.end();\n  else res.end(data);\n}","typeGuard":"function isWritableChunk(c: unknown): c is string | Uint8Array {\n  return typeof c === 'string' || (c instanceof Uint8Array);\n}","tryCatchPattern":"try {\n  res.write(data);\n} catch (e) {\n  if (e?.code === 'ERR_STREAM_NULL_VALUES' && data === null) return true; // ignore empty\n  throw e;\n}","preventionTips":["Use `?? ''` (not `|| null`) when defaulting optional payloads","Prefer res.end() with no arguments for empty bodies","Search codebases for write(null)/end(null) after Node upgrades"],"tags":["http","stream","null","node-compat"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}