“JavaScript heap out of memory” with Excel files

You export a report to .xlsx, or read an uploaded spreadsheet, and Node stops with:

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory

The usual cause is that the whole workbook is held in memory at once. A spreadsheet library keeps an object for every cell, which takes many times the file's size: in our read benchmark, a 6.4 MB file took 549 MB of memory with SheetJS and 667 MB with ExcelJS's normal reader. A file ten times bigger needs more than Node's default heap.

1. Check where the rows come from

A streaming writer can't help if your code first loads every row from the database into an array. Fetch the rows in pages or through a cursor, and hand them to the writer as you go. The examples below use db.users.cursor() as a stand-in for that.

2. If you use ExcelJS: switch to its streaming classes

workbook.xlsx.writeFile, writeBuffer and readFile build the whole workbook in memory. ExcelJS has separate streaming classes. For writing, commit each row as you add it:

import ExcelJS from 'exceljs';

const workbook = new ExcelJS.stream.xlsx.WorkbookWriter({ filename: 'big.xlsx' });
const sheet = workbook.addWorksheet('Data');
for await (const user of db.users.cursor()) sheet.addRow([user.id, user.name]).commit();
sheet.commit();
await workbook.commit();

For reading:

const reader = new ExcelJS.stream.xlsx.WorkbookReader('big.xlsx');
for await (const worksheet of reader) {
  for await (const row of worksheet) console.log(row.values);
}

The streaming classes don't support everything the normal ones do, so check the features you rely on. ExcelJS's last release was in October 2023.

3. If you use SheetJS: dense mode helps, but only so far

SheetJS reads the whole workbook before you see a cell, and builds the whole file before writing it. Dense mode stores cells in arrays rather than one object per cell address, which uses less memory:

const wb = XLSX.readFile('big.xlsx', { dense: true });

It doesn't make reading or writing .xlsx stream, so very large files still don't fit. Note also that the xlsx package on npm is stuck at 0.18.5 from 2022, with two published security advisories; newer versions are installed from the SheetJS CDN.

4. Raising the heap limit

node --max-old-space-size=4096 app.js buys time, but the memory needed still grows with the file, and serverless functions and containers have a hard limit you can't raise this way.

5. Use a library that streams by default

I wrote XlsxFlow for this case, so take this section with that in mind. It reads and writes .xlsx one row at a time with a single API, so there's no separate streaming mode to switch to. Its CI writes and reads back 10 million cells with Node's heap capped at 32 MB. Writing:

import { createWriteStream } from 'node:fs';
import { Writable } from 'node:stream';
import { SheetWriter } from '@xlsxflow/core';

async function* rows() {
  yield ['ID', 'Name'];
  for await (const user of db.users.cursor()) yield [user.id, user.name];
}
await new SheetWriter().addSheet('Users', rows()).write()
  .pipeTo(Writable.toWeb(createWriteStream('users.xlsx')));

Reading:

import { XlsxFlow } from '@xlsxflow/core';

for await (const row of await XlsxFlow.readFile('big.xlsx')) {
  console.log(row.rowNumber, row.cells);
}

It also runs in browsers, Bun, Deno and Cloudflare Workers. What it doesn't do: jump to any cell at any time, since rows stream past once. If you're moving from another library, there are guides for ExcelJS and SheetJS, and recipes for Express, Next.js, Workers and database exports.

Need a one-off conversion rather than code? The CSV to Excel and Excel to CSV converters stream in your browser.