4

When I tried to use exceljs in Angular-6 like so

import * as excel from 'exceljs'

createExcel() {
  let workbook = new excel.Workbook()
}

even I just initialize a class and got this error. If I comment out this line let workbook = new excel.Workbook() then error gone

./node_modules/fs-constants/browser.js Module not found: Error: Can't resolve 'constants' in 'D:\Developer\spire-client\node_modules\fs-constants'

enter image description here

WasiF
  • 26,101
  • 16
  • 120
  • 128

2 Answers2

10

You could import it as follow

import * as Excel from 'exceljs';

after that, you can use Exceljs:

const myWorkbook = new Excel.Workbook()

or

import * as Excel from "exceljs/dist/exceljs.min.js";
import * as ExcelProper from "exceljs";

let workbook: ExcelProper.Workbook = new Excel.Workbook();

As long as you just use the import from exceljs for type definitions and only use functions from the import from exceljs/dist/exceljs.min.js, the app will run just fine and you still get type safety.

ref: https://www.npmjs.com/package/exceljs#create-a-workbook https://github.com/guyonroche/exceljs/issues/348#issuecomment-320690232

Chandu
  • 2,053
  • 3
  • 25
  • 39
  • 1
    as you proposed me this solution, at the same time I found it on [https://github.com/guyonroche/exceljs](https://github.com/guyonroche/exceljs/issues/348#issuecomment-320690232). Thanks – WasiF Sep 14 '18 at 12:41
  • Yes sir, most of the things like configurations and import you can get it from github issues page itself. Since I worked with Data Driven (Viz) websites. I used exceljs and glad you found the solution. and I added the ref too – Chandu Sep 14 '18 at 12:43
  • If above doesn't work use this import * as ExcelJS from "exceljs/dist/exceljs.min.js"; let workbook = new ExcelJS.Workbook(); Accessing it with name ExcelJS solved issue. Hope this helps. – visrey Apr 19 '20 at 21:33
-1

Import like this

import * as Excel from "exceljs/dist/exceljs.min.js";

and declare

const workbook = new Excel.Workbook();

It will work.

Abhishek Kumar
  • 820
  • 10
  • 18