Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
  }
}
like image 845
Sourabh Banka Avatar asked Oct 24 '25 07:10

Sourabh Banka


2 Answers

I got it working by using:

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

rather than

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

like image 175
Ravi Avatar answered Oct 25 '25 21:10

Ravi


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
like image 41
bwalshy Avatar answered Oct 25 '25 22:10

bwalshy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!