{"record":{"id":"cffa60b12b1670af","repo":"denoland/deno","slug":"err-invalid-arg-value","errorCode":"ERR_INVALID_ARG_VALUE","errorMessage":"The argument 'buffer' is empty and cannot be written. Received ${value}","messagePattern":"The argument 'buffer' is empty and cannot be written\\. Received (.+?)","errorType":"validation","errorClass":"NodeTypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/_fs/_fs_read.ts","lineNumber":149,"sourceCode":"    validateInteger(offset, \"offset\", 0);\n  }\n\n  (length as number) |= 0;\n\n  if (position == null) {\n    position = -1;\n  } else {\n    validatePosition(position, \"position\", length as number);\n  }\n\n  if (length === 0) {\n    return lazyProcess().default.nextTick(function tick() {\n      callback!(null, 0, buffer);\n    });\n  }\n\n  if (getByteLength(buffer as ArrayBufferView) === 0) {\n    throw new ERR_INVALID_ARG_VALUE(\n      \"buffer\",\n      buffer,\n      \"is empty and cannot be written\",\n    );\n  }\n\n  validateOffsetLengthRead(\n    offset,\n    length,\n    getByteLength(buffer as ArrayBufferView),\n  );\n\n  // BigInt avoids precision loss for positions > 2^53. -1n means current pos.\n  const readPos = position != null && position >= 0\n    ? BigInt(position as number | bigint)\n    : -1n;\n  PromisePrototypeThen(\n    op_node_fs_read_deferred(","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/denoland/deno/blob/89f33cbef296a2b287f323d42de54c871fa69c77/ext/node/polyfills/_fs/_fs_read.ts#L131-L167","documentation":"In callback-style fs.read (ext/node/polyfills/_fs/_fs_read.ts), after argument normalization the zero-length fast path only fires when length === 0. If length is non-zero but the supplied buffer's byte length is 0, it throws ERR_INVALID_ARG_VALUE('buffer', buffer, 'is empty and cannot be written'). The read would have nowhere to put bytes.","triggerScenarios":"fs.read(fd, Buffer.alloc(0), 0, 5, null, cb); fs.read(fd, new Uint8Array(0), { length: 1 }, cb); any call where an explicitly passed zero-length Buffer/TypedArray is combined with a positive length (explicit, or defaulted from a non-empty offset).","commonSituations":"Allocating a buffer from a computed size that rounds to 0 (e.g., fileSize - alreadyRead when the file is fully consumed); reusing a buffer that was subarray()'d to nothing; off-by-one length math.","solutions":["Check the buffer length before reading: if (buf.length === 0) skip or allocate a real size","Fix the size arithmetic that produced an empty buffer (e.g., remaining-bytes calculation)","For 'read whatever is left', pass a properly sized buffer and let nread report the actual bytes"],"exampleFix":"// before\nfs.read(fd, buf.subarray(0, remaining), 0, remaining, pos, cb); // remaining = 0\n\n// after\nif (remaining <= 0) return cb(null, 0, buf);\nfs.read(fd, buf.subarray(0, remaining), 0, remaining, pos, cb);","handlingStrategy":"validation","validationCode":"if (buffer.length === 0) {\n  return callback(null, 0, buffer); // nothing to read into\n}","typeGuard":"const hasCapacity = (buf) => buf != null && buf.byteLength > 0;","tryCatchPattern":null,"preventionTips":["Compute chunk size as Math.max(0, remaining) and stop the read loop at 0","Allocate read buffers from validated positive sizes","Pass length 0 (not a positive length with an empty buffer) when you want the no-op path"],"tags":["node-compat","fs","read","buffer","validation"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}