denoland/deno · error · NodeTypeError

ERR_STREAM_NULL_VALUES

ERR_STREAM_NULL_VALUES

Error message

May not write null values to stream

What it means

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.

Source

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

    return object && object._writableState instanceof WritableState;
  },
});

// Otherwise people can pipe Writable streams, which is just wrong.
Writable.prototype.pipe = function () {
  errorOrDestroy(this, new ERR_STREAM_CANNOT_PIPE());
};

function _write(stream, chunk, encoding, cb) {
  const state = stream._writableState;

  if (cb == null || typeof cb !== "function") {
    cb = nop;
  }

  if (chunk === null) {
    throw new ERR_STREAM_NULL_VALUES();
  }

  if ((state[kState] & kObjectMode) === 0) {
    if (!encoding) {
      encoding = (state[kState] & kDefaultUTF8Encoding) !== 0
        ? "utf8"
        : state.defaultEncoding;
    } 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";

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Filter null chunks before writing: if (chunk !== null) ws.write(chunk)
  2. If null means 'done', call ws.end() with no arguments instead of writing null
  3. Write '' or Buffer.alloc(0) when a zero-length write is intended

Example fix

// before
ws.write(row.optionalField); // optionalField can be null

// after
if (row.optionalField !== null) ws.write(row.optionalField);
Defensive patterns

Strategy: validation

Validate before calling

const chunk = next();
if (chunk === null) {
  // skip, or treat as end: ws.end(); return;
}
ws.write(chunk);

Type guard

function isWritableChunk(c) {
  return c !== null && (typeof c === 'string' || Buffer.isBuffer(c) || ArrayBuffer.isView(c));
}

Try / catch

ws.on('error', (err) => {
  if (err.code === 'ERR_STREAM_NULL_VALUES') {
    console.error('null chunk produced upstream — fix the source');
  }
});

Prevention

When it happens

Trigger: 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.

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

Related errors


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