Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find namespace 'NodeJS'

If i run this code, i get an error, Cannot find namespace 'NodeJS'.

  public exportExcel(jsonData: any[], excelFileName: string): void {
    //Excel Title, Header, Data
    const header: string[] =  Object.keys(jsonData[0]);
    const data = jsonData;

    //Create workbook and worksheet
    let workbook = new Workbook();
    let worksheet = workbook.addWorksheet(excelFileName);

    //Add Header Row
    let headerRow = worksheet.addRow(header);

    // Cell Style : Fill and Border
    headerRow.eachCell((cell, number) => {
      cell.fill = {
        type: 'pattern',
        pattern: 'solid',
        fgColor: { argb: 'FFFFFF00' },
        bgColor: { argb: 'FF0000FF' }
      }
      cell.border = { top: { style: 'thin' }, left: { style: 'thin' }, bottom: { style: 'thin' }, right: { style: 'thin' } }
    })
    // Add Data and Conditional Formatting
    data.forEach((element) => {
      let eachRow = [];
      header.forEach((headers) => {
        eachRow.push(element[headers])
      })
      if (element.isDeleted === "Y") {
        let deletedRow = worksheet.addRow(eachRow);
        deletedRow.eachCell((cell, number) => {
          cell.font = { name: 'Calibri', family: 4, size: 11, bold: false, strike: true };
        })
      } else {
        worksheet.addRow(eachRow);
      }
    })

...

ERROR in node_modules/exceljs/index.d.ts(1648,34): error TS2503: Cannot find namespace 'NodeJS'.

like image 881
kuku Avatar asked Oct 03 '19 17:10

kuku


2 Answers

Solution targetting Angular8+ Projects:

This is a known bug, caused due to the incompatibility of exceljs with the version of @types/node. I faced similar issue with Angular 10.

2 possible solutions exists:

  1. Recommended: Update your tsconfig.app.json file with "types": ["node"]

enter image description here

  1. If you are okay without having type support, then you can simply use the below import:
    import * as Excel from "exceljs/dist/exceljs.min.js";
like image 68
RICHARD ABRAHAM Avatar answered Nov 10 '22 19:11

RICHARD ABRAHAM


Just Open index.d.ts file from yours exceljs node_modules

enter image description here

and replace this line dictionary: Buffer | NodeJS.TypedArray | DataView | ArrayBuffer; // deflate/inflate only, empty dictionary by default

with this

dictionary: Buffer | DataView | ArrayBuffer; // deflate/inflate only, empty dictionary by default

and then just ng serve

like image 42
user3894404 Avatar answered Nov 10 '22 18:11

user3894404