node.jsangulartypescriptnpmsheetjs

Cannot find module 'xlsx'


Am getting this while making build using jenkins on build server but i am trying this on local machine then it working fine with error

15:07:39 "",
15:07:39 "",
15:07:39 "ERROR in src/services/excel.service.ts:2:23 - error TS2307: Cannot find module 'xlsx'.",
15:07:39 "",
15:07:39 "2 import * as XLSX from 'xlsx';",
15:07:39 " ~~~~~~"
15:07:39 

install xlsx using npm install xlsx

And import xlsx module. import * as XLSX from 'xlsx';

import { Injectable } from '@angular/core';
import * as XLSX from 'xlsx';
import * as _ from 'lodash';

@Injectable({
  providedIn: 'root'
})
export class ExcelService {

  constructor() { }
  wopts: XLSX.WritingOptions = { bookType: 'xlsx', type: 'array' };
  exportAsExcelFile(json: any, fileName:any): void {
    /* generate worksheet */
    // const ws: XLSX.WorkSheet = XLSX.utils.aoa_to_sheet(this.data);
    const ws: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json);

    /* generate workbook and add the worksheet */
    const wb: XLSX.WorkBook = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');

    /* save to file */
    XLSX.writeFile(wb, fileName);
  }

}

Solution

  • import xlsx from 'xlsx/xlsx';

    This line works for me on node/typescript environment.