denoland/deno · error · NodeRangeError

ERR_FS_FILE_TOO_LARGE

ERR_FS_FILE_TOO_LARGE

Error message

File size (${size}) is greater than 2 GB

What it means

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.

Solutions

  1. Stream the file instead: fs.createReadStream(path) or reading with fs.read() at explicit offsets
  2. Check fs.statSync(path).size first and branch to a streaming path above 2 GB
  3. Process incrementally with for await (const chunk of fs.createReadStream(path))
  4. Split the source file if consumers genuinely need whole-file buffers

Example fix

// before
const data = await fs.promises.readFile(hugePath); // ERR_FS_FILE_TOO_LARGE

// after
const chunks = [];
for await (const chunk of fs.createReadStream(hugePath)) chunks.push(chunk);
Defensive patterns

Strategy: validation

Validate before calling

const { size } = await fs.promises.stat(path);
const MAX = 2 ** 31 - 1; // 2 GB readFile cap
if (size > MAX) {
  // stream it instead of readFile
}

Try / catch

try {
  return await fs.promises.readFile(path);
} catch (err) {
  if ((err as NodeJS.ErrnoException).code === 'ERR_FS_FILE_TOO_LARGE') {
    return await streamAll(fs.createReadStream(path)); // fallback path
  }
  throw err;
}

Prevention

When it happens

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

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

Related errors


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

Appendix: source

Thrown at ext/node/polyfills/fs.ts:733

    TypedArrayPrototypeSet(contents, buf, n);
    n += TypedArrayPrototypeGetByteLength(buf);
  }

  return contents;
}

async function readFileFromFd(fd: number, options?: FileOptions) {
  const signal = options?.signal;
  readFileCheckAborted(signal);

  const statFields = op_node_fs_fstat_sync(fd);
  readFileCheckAborted(signal);

  const isFile = statFields.isFile;
  const size = isFile ? statFields.size : 0;

  if (size > kIoMaxLength) {
    throw new ERR_FS_FILE_TOO_LARGE(size);
  }

  if (isFile && size > 0) {
    // Known size: read into a single buffer with an advancing offset.
    // Mirrors Node's readFileHandle which avoids the subarray-aliasing trap
    // by writing successive reads into different regions of one buffer.
    const buffer = new Uint8Array(size);
    let totalRead = 0;
    while (totalRead < size) {
      readFileCheckAborted(signal);
      const slice = TypedArrayPrototypeSubarray(buffer, totalRead);
      // Use the deferred op so we yield to the event loop between reads,
      // allowing abort signals scheduled via lazyProcess().default.nextTick to fire.
      const nread = await op_node_fs_read_deferred(fd, slice, -1n);
      if (nread === 0) break;
      totalRead += nread;
    }
    readFileCheckAborted(signal);

View on GitHub (pinned to 9ad36f7a2c)