{"record":{"id":"04303dc59877b639","repo":"denoland/deno","slug":"err-fs-file-too-large","errorCode":"ERR_FS_FILE_TOO_LARGE","errorMessage":"File size (${size}) is greater than 2 GB","messagePattern":"File size \\((.+?)\\) is greater than 2 GB","errorType":"exception","errorClass":"NodeRangeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/fs.ts","lineNumber":733,"sourceCode":"    TypedArrayPrototypeSet(contents, buf, n);\n    n += TypedArrayPrototypeGetByteLength(buf);\n  }\n\n  return contents;\n}\n\nasync function readFileFromFd(fd: number, options?: FileOptions) {\n  const signal = options?.signal;\n  readFileCheckAborted(signal);\n\n  const statFields = op_node_fs_fstat_sync(fd);\n  readFileCheckAborted(signal);\n\n  const isFile = statFields.isFile;\n  const size = isFile ? statFields.size : 0;\n\n  if (size > kIoMaxLength) {\n    throw new ERR_FS_FILE_TOO_LARGE(size);\n  }\n\n  if (isFile && size > 0) {\n    // Known size: read into a single buffer with an advancing offset.\n    // Mirrors Node's readFileHandle which avoids the subarray-aliasing trap\n    // by writing successive reads into different regions of one buffer.\n    const buffer = new Uint8Array(size);\n    let totalRead = 0;\n    while (totalRead < size) {\n      readFileCheckAborted(signal);\n      const slice = TypedArrayPrototypeSubarray(buffer, totalRead);\n      // Use the deferred op so we yield to the event loop between reads,\n      // allowing abort signals scheduled via lazyProcess().default.nextTick to fire.\n      const nread = await op_node_fs_read_deferred(fd, slice, -1n);\n      if (nread === 0) break;\n      totalRead += nread;\n    }\n    readFileCheckAborted(signal);","sourceCodeStart":715,"sourceCodeEnd":751,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/fs.ts#L715-L751","documentation":"readFileFromFd implements readFile from an open fd. It fstats the file and, for regular files with a known size, compares that size against kIoMaxLength (the polyfill's 2 GB single-buffer cap). A file larger than the cap cannot be returned as one Buffer, so it throws ERR_FS_FILE_TOO_LARGE instead of attempting an oversized allocation, matching Node's readFileHandle behavior.","triggerScenarios":"fs.readFile()/fsPromises.readFile on a regular file larger than 2 GiB: large datasets, ISO/VM images, database dumps, video files, or multi-gigabyte log archives.","commonSituations":"Data scripts that readFile a giant CSV/JSON export; loading model files; code that passed on small test fixtures then hits production-sized files; memory-constrained containers where the whole-file buffer was already risky.","solutions":["Stream the file instead: fs.createReadStream(path) or reading with fs.read() at explicit offsets","Check fs.statSync(path).size first and branch to a streaming path above 2 GB","Process incrementally with for await (const chunk of fs.createReadStream(path))","Split the source file if consumers genuinely need whole-file buffers"],"exampleFix":"// before\nconst data = await fs.promises.readFile(hugePath); // ERR_FS_FILE_TOO_LARGE\n\n// after\nconst chunks = [];\nfor await (const chunk of fs.createReadStream(hugePath)) chunks.push(chunk);","handlingStrategy":"validation","validationCode":"const { size } = await fs.promises.stat(path);\nconst MAX = 2 ** 31 - 1; // 2 GB readFile cap\nif (size > MAX) {\n  // stream it instead of readFile\n}","typeGuard":null,"tryCatchPattern":"try {\n  return await fs.promises.readFile(path);\n} catch (err) {\n  if ((err as NodeJS.ErrnoException).code === 'ERR_FS_FILE_TOO_LARGE') {\n    return await streamAll(fs.createReadStream(path)); // fallback path\n  }\n  throw err;\n}","preventionTips":["Stat and check size before any readFile on user-supplied paths","Default to createReadStream for anything that can plausibly exceed 2 GB","Never assume a whole-file Buffer is required — most parsers accept chunked input"],"tags":["fs","readfile","file-size","memory"],"backgroundTag":"file-too-large","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}