Search code examples
reactjsexceljs

import Excel from "exceljs" throwing error on browser TypeError: Cannot read property 'prototype' of undefined


I am building my webpage using react. I want to download my reducer's store data into a xlxs file. For that I am using exceljs package. I have install the exceljs using npm i exceljs.

But when I tried to import exceljs to my componenet I am getting TypeError: Cannot read property 'prototype' of undefined on the screen.

I am importing using command import Excel from "exceljs";.

Please have a look to screenshoot for better error view that I am getting on my screen. enter image description here enter image description here

my package.json is:

{
  "name": "xxxxxxx_xxxxx",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "exceljs": "^1.6.0",
    "react": "^16.4.1",
    "react-dom": "^16.4.1",
    "react-intl": "^2.4.0",
    "react-redux": "^5.0.7",
    "react-router": "^4.3.1",
    "react-router-dom": "^4.3.1",
    "react-scripts": "1.1.4",
    "redux": "^4.0.0",
    "redux-logger": "^3.0.6",
    "redux-thunk": "^2.3.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build:langs": "NODE_ENV='production' ./node_modules/.bin/babel src --out-dir lib",
    "build:locale": "NODE_ENV='production' babel-node scripts/translator.js",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-plugin-react-intl": "^2.4.0"
  }
}

Solution

  • It seems like ExcelJS's exports are a bit weird for browser. I got it to work by doing:

    import Excel from "exceljs/dist/es5/exceljs.browser";
    

    Then you can use Excel however you like. I.e:

    console.log('excel', Excel);  // Check that it exists
    var workBook = new Excel.Workbook(); // Make a new Workbook