exceljs: Can't use this package on browser

I’m trying to use exceljs on browser to read some XLSX files. But, when I tried to import using

import Excel from 'exceljs';

I received an error on build because can’t resolve fs module:

ERROR in ./node_modules/exceljs/dist/es5/csv/csv.js
Module not found: Error: Can't resolve 'fs' in '/my-project/node_modules/exceljs/dist/es5/csv'
ERROR in ./node_modules/exceljs/dist/es5/utils/utils.js
Module not found: Error: Can't resolve 'fs' in '/my-project/node_modules/exceljs/dist/es5/utils'
ERROR in ./node_modules/exceljs/dist/es5/xlsx/xlsx.js
Module not found: Error: Can't resolve 'fs' in '/my-project/node_modules/exceljs/dist/es5/xlsx'
...

Searching about how is the correct way to import on browser, I found this example, and following it I tried importing using

const ExcelJS = require('exceljs/dist/es5/exceljs.browser');

But this command also didn’t work, returning the same errors.

Also I found a related open issue about this problem.

Then… is there a way to import exceljs on browser?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 24 (3 by maintainers)

Most upvoted comments

I solved this by adding: declare const ExcelJS: any;

and in angular.json: "scripts": [ ... "node_modules/exceljs/dist/exceljs.min.js" ]

Change your import to ExcelJS, like this:

import * as ExcelJS from 'exceljs/dist/exceljs.min.js';

Hi @abmj1979, Thanks, it’s working now. What I didn’t understand is why the line declare const ExcelJS: any; should be there.

Also, why we need to add exceljs.min.js in angular.json if we are already using it as an import in our service.

is it possible to use in a browser to read a excel file with no angular, just a single html page referencning the js files? i have tried and i keep getting a error

<script src="assets/exceljs/dist/exceljs.min.js"></script>

$(document).ready(function () { //var workbook = new ExcelJS.Workbook();

// var worksheet = workbook.addWorksheet("Sheet1");


//   workbook.xlsx.readFile("exceldata.xlsx").then(function(data) {
//     console.log("hss");

//   });

var workbook = new ExcelJS.Workbook();
workbook.xlsx.readFile("exceldata.xlsx")
    .then(function (data) {
        // var worksheet = workbook.getWorksheet("WPA ext libs 2017.10.05");

        // worksheet.eachRow(function (row, rowNumber) {
            // row_values contains the values, (first column is indexed by  
            var row_values = row_1.values;
            // Now you can access the columns directly by the column index
            Console.log("Value of Column B is : " + row_values[2])
    //     }
    });

}); exceljs.min.js:3 Uncaught (in promise) TypeError: i.exists is not a function at exceljs.min.js:3 at new Promise (<anonymous>) at Object.exists (exceljs.min.js:3) at i.value (exceljs.min.js:3)

You can put this in each component or file where you want to use ExcelJS (and remove for example import * as ExcelJS from 'exceljs')

Change your import to ExcelJS, like this:

import * as ExcelJS from 'exceljs/dist/exceljs.min.js';

Actually, my PR should solve your issue too: https://github.com/exceljs/exceljs/pull/806 One line in package.json allows to import browser version when project is built using webpack or any other tool that respects “browser” property. It is merged, but there were no new releases yet.