xlsx-stream icon indicating copy to clipboard operation
xlsx-stream copied to clipboard

ERROR (node:2) [DEP0005] Using wrong Buffer in library

Open BrenoRev opened this issue 11 months ago • 0 comments

When you're running the Lambda function, the deprecation warning is being treated as an error. This is because Lambda handles unhandled promise rejections or exceptions in a way that might trigger the error handler. You can address this by catching and silencing the warning so it doesn't trigger the Lambda error handler, while still allowing the code to execute normally.

"ERROR (node:2) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead. (Use node --trace-deprecation ... to show where the warning was created)"

Node v20.11.0 "xlsx-write-stream": "1.0.3"

import { createWriteStream } from 'fs';
import XLSXWriteStream from 'xlsx-write-stream';

export const createExcelFile = (data: {
  xlsxTemplate: Record<string, any> | Array<Record<string, any>>;
  filePath: string;
}): Promise<void> => {
  return new Promise((resolve, reject) => {
    const { xlsxTemplate, filePath } = data;

    const jsonArray: Array<Record<string, any>> = Array.isArray(xlsxTemplate)
      ? xlsxTemplate
      : [xlsxTemplate];

    const jsonKeys: string[] = Object.keys(jsonArray[0]);

    const rows: any[][] = jsonArray.map(row => {
      return jsonKeys.map(key => {
        let value = row[key];

        if (
          value == null ||
          value === undefined ||
          value == 'undefined' ||
          value == 'null' ||
          value == 'NaN'
        ) {
          value = '-';
        }

        return value;
      });
    });

    const xlsxStream = new XLSXWriteStream();
    const writeStream = createWriteStream(filePath);

    xlsxStream.pipe(writeStream);

    xlsxStream.write(jsonKeys);

    rows.forEach(row => {
      xlsxStream.write(row);
    });

    xlsxStream.end();

    writeStream.on('finish', () => {
      console.log('Arquivo XLSX criado com sucesso:', filePath);

      resolve();
    });
    writeStream.on('error', err => {
      console.error('Erro na escrita do arquivo XLSX:', err);
      reject(err);
    });
  });
};

Image

BrenoRev avatar Mar 06 '25 18:03 BrenoRev