Skip to content

[BUG] EMFILE: too many open files error When Adding Multiple Images to a Worksheet #2623

@shuntagami

Description

@shuntagami

🐛 Bug Report

exceljs: 4.4.0, Node.js: 20.x

Steps To Reproduce

https://github.com/shuntagami/excel-util Here I reproduce with docker-compose, specifying ulimits

import ExcelJS from "exceljs";
import { readFileSync } from "fs";

async function main() {
  const workbook = new ExcelJS.Workbook();
  const targetSheet = workbook.addWorksheet("Target Sheet");

  for (let i = 0; i < 1000; i++) {
    const imagePath = `images/image-${i}.jpeg`
    console.log(imagePath);

    const imageId = workbook.addImage({
      filename: imagePath,
      // buffer: readFileSync(imagePath),
      extension: 'jpeg',
    });
    console.log(`image added to workbook, imageId: ${imageId}`);


    targetSheet.addImage(imageId, {
      tl: { col: 1, row: (i + 1)*3 },
      ext: { width: 100, height: 100 }
    });
    console.log('image added to worksheet');

  }
  const path = `./results/${Date.now()}.xlsx`;
  console.log(`writing data to ${path}`);

  await workbook.xlsx.writeFile(path)
  console.log(`File written to ${path}`);
}

main().catch((error) => console.error(error));

the problem is arround here below

const imageId = workbook.addImage({
      // filename: imagePath,
      buffer: readFileSync(imagePath),
      extension: 'jpeg',
    });

when I use filename: imagePath, it will raise the error, but if I use buffer: readFileSync(imagePath), not reproduce so forget to close file anywhere ?

The error msg is below

images/image-998.jpeg
image added to workbook, imageId: 998
image added to worksheet
images/image-999.jpeg
image added to workbook, imageId: 999
image added to worksheet
writing data to ./results/1702012108045.xlsx
[Error: EMFILE: too many open files, open 'images/image-275.jpeg'] {
  errno: -24,
  code: 'EMFILE',
  syscall: 'open',
  path: 'images/image-275.jpeg'
}

The expected behaviour:

Work both on using filename and buffer

Possible solution (optional, but very helpful):

I want to fix this issue but still don't know where to fix it

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions