0

I'm still new to NodeJS and am getting a MaxListenersExceededWarning: Possible EventEmitter memory leak detected message when I call the following function:

function zipUpFile() {
  const folderPath = "./dist";
  const zip = zlib.createGzip();
  const zipWriteStream = fs.createWriteStream("signature-templates.zip");

  fs.readdir(folderPath, (error, files) => {
    if (error) {
      console.error(error);
      return;
    }

    const htmlFiles = files.filter((file) => file.endsWith(".html"));

    // loop through the HTML files and pipe each one to the zip file
    htmlFiles.forEach((htmlFile) => {
      fs.createReadStream(`${folderPath}/${htmlFile}`)
        .pipe(zip)
        .pipe(zipWriteStream);
    });
  });
}

I'm trying to understand how event emitters work but am still having trouble finding the cause. Any help is greatly appreciated!

  • 1
    You cannot pipe multiple read streams into the same `zip` stream, nor into the same write stream. – Heiko Theißen Dec 23 '22 at 16:24
  • @HeikoTheißen could you explain how I could avoid that in my code? I'm assuming the problem is that I'm creating a new read stream for each individual html file, and each of those is being piped into the same `zipWriteStream`, correct? I tried creating the write stream inside my `forEach` loop, instead but I'm still having the same errors. Thanks for the reply! – RenaissanceMan Dec 23 '22 at 21:31

0 Answers0