9

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'.

kuku
  • 91
  • 1
  • 1
  • 2
  • https://github.com/exceljs/exceljs/issues/971 can help – Rajat Oct 03 '19 at 17:16
  • ok thank you i will try – kuku Oct 03 '19 at 17:18
  • @dev but i get this information, what it´s mean? 100 unchanged chunks chunk {main} main.js, main.js.map (main) 429 kB [initial] [rendered] chunk {vendor} vendor.js, vendor.js.map (vendor) 9.43 MB [initial] [rendered] Time: 6775ms – kuku Oct 03 '19 at 17:27
  • Not sure about those lines. but did you tried the suggestions mentioined in the above link and did it helped? – Rajat Oct 03 '19 at 17:40
  • Yes i tried and get in the Chrome console this errors: core.js:9110 ERROR Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[EventOverviewPageComponent -> ExportService]: StaticInjectorError(Platform: core)[EventOverviewPageComponent -> ExportService]: NullInjectorError: No provider for ExportService! – kuku Oct 03 '19 at 17:46
  • Please check it out https://stackoverflow.com/questions/49776562/uncaught-in-promise-error-staticinjectorerrorappmoduleoptions || https://stackoverflow.com/questions/50718852/uncaught-in-promise-error-staticinjectorerrorappmoduleemployeservice-h – Rajat Oct 03 '19 at 17:50
  • @dev THANK YOU VERY MUCH; THANKS;THANKS;THANKS – kuku Oct 03 '19 at 18:09
  • update @types/node with `npm i -D @types/node@14.17.6` – WITO Sep 19 '21 at 03:47

2 Answers2

18

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";
RICHARD ABRAHAM
  • 2,218
  • 20
  • 26
2

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

  • 1
    LOL! What about the next time they install their dependencies??? Would you expect them to change the code in their node modules every time? – Jake Smith Jun 30 '20 at 16:12
  • you can use package-patch if you HAVE to go with this approach – MCH Dec 17 '20 at 13:02