denoland/deno · error · NodeTypeError

ERR_STREAM_NULL_VALUES

ERR_STREAM_NULL_VALUES

Error message

May not write null values to stream

What it means

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.

Source

Thrown at ext/node/polyfills/_http_outgoing.ts:1357

    !msg._removedContLen &&
    !msg.chunkedEncoding &&
    !msg.hasHeader("transfer-encoding")
  );
}

function write_(
  msg: any,
  chunk: any,
  encoding: string | null,
  callback: any,
  fromEnd: boolean,
): boolean {
  if (typeof callback !== "function") {
    callback = nop;
  }

  if (chunk === null) {
    throw new ERR_STREAM_NULL_VALUES();
  } else if (typeof chunk !== "string" && !isUint8Array(chunk)) {
    throw new ERR_INVALID_ARG_TYPE(
      "chunk",
      ["string", "Buffer", "Uint8Array"],
      chunk,
    );
  }

  let err;
  if (msg.finished) {
    err = new ERR_STREAM_WRITE_AFTER_END();
  } else if (msg.destroyed) {
    err = new ERR_STREAM_DESTROYED("write");
  }

  if (err) {
    if (!msg.destroyed) {
      _onError(msg, err, callback);

View on GitHub (pinned to 89f33cbef2)

Solutions

  1. End without a body: res.end()
  2. Send an empty payload explicitly: res.end('') or res.end(Buffer.alloc(0))
  3. Fix the default: `res.end(data ?? '')` instead of `data || null`

Example fix

// before
res.end(data || null); // data is '' -> end(null) throws ERR_STREAM_NULL_VALUES

// after
res.end(data ?? '');
Defensive patterns

Strategy: validation

Validate before calling

// never pass null as a chunk
function safeEnd(res: http.ServerResponse, data?: string | Buffer | null) {
  if (data === null || data === undefined) res.end();
  else res.end(data);
}

Type guard

function isWritableChunk(c: unknown): c is string | Uint8Array {
  return typeof c === 'string' || (c instanceof Uint8Array);
}

Try / catch

try {
  res.write(data);
} catch (e) {
  if (e?.code === 'ERR_STREAM_NULL_VALUES' && data === null) return true; // ignore empty
  throw e;
}

Prevention

When it happens

Trigger: res.write(null), res.end(null), or req.end(null, cb); classic pattern `res.end(data || null)` where data is empty.

Common situations: 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().

Related errors


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