Sheetjs: Dependency Issues

Created on 9 May 2014  路  7Comments  路  Source: SheetJS/sheetjs

I am having some difficulties getting the library to work properly from the browser.
I downloaded the latest build and tried including the core, full versions, as well as the jszip with xlsx and I get the same error (listed below).
ReferenceError: require is not defined
var XLSX = require('xlsx');

TypeError: _fs is undefined
d = _fs.readFileSync(data).toString('base64');

Any thoughts as to what dependency I might be missing?
Thanks.

Most helpful comment

@JustinJmnz readFile only works in node, not in the browser. You need to pull in data and use XLSX.read. See https://github.com/SheetJS/js-xlsx#parsing-workbooks in the README for some examples and demo links. In particular, http://oss.sheetjs.com/js-xlsx/ajax.html uses XHR and http://oss.sheetjs.com/js-xlsx/ uses the HTML5 File API

All 7 comments

@pjpastrana the require line is necessary in places like node or when using RequireJS. In the browser, if you just use script tags, you don't need that line.

I will update the README, but in the interim can you try:

if(typeof XLSX === 'undefined' && typeof require !== 'undefined') XLSX = require('xlsx');

This line tries to require XLSX only if it's not defined beforehand.

I keep getting the
TypeError: _fs is undefined
d = _fs.readFileSync(data).toString('base64');
error.

Based on the code starting at line 540, the typeof exports is 'undefined'. Where is exports supposed to be defined?
Thanks for your help.

@pjpastrana that whole code block should not be run in the browser.

Can you test a few things for me:

1) open http://sheetjs.com/testxlsx.html and see if there are any errors in the console (this only includes <script type="text/javascript" src="http://oss.sheetjs.com/js-xlsx/xlsx.core.min.js"></script>)

2) test http://oss.sheetjs.com/js-xlsx/ajax.html -- this is a simple demo that uses the jszip script and the xlsx script.

If both work, could you post a small case that fails?

@pjpastrana any updates?

@pjpastrana closing for now, please reply or open a new issue

@SheetJSDev
Hey, I was just going through this because I had the same problem. This solution didn't work for me. After adding the CDN, I still get the same error: TypeError: _fs is undefined xlsx.core.min.js:15:3896

This is what my JavaScript looks like,

   if(typeof XLSX === 'undefined' && typeof require !== 'undefined') XLSX = require('xlsx');

var workbook = XLSX.readFile('menu.xlsx');

var sheet_name_list = workbook.SheetNames;

console.log(XLSX.utils.sheet_to_json(workbook.Sheets[sheet_name_list[0]]))

@JustinJmnz readFile only works in node, not in the browser. You need to pull in data and use XLSX.read. See https://github.com/SheetJS/js-xlsx#parsing-workbooks in the README for some examples and demo links. In particular, http://oss.sheetjs.com/js-xlsx/ajax.html uses XHR and http://oss.sheetjs.com/js-xlsx/ uses the HTML5 File API

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gustavosimil picture gustavosimil  路  3Comments

Alex0007 picture Alex0007  路  3Comments

happy0088 picture happy0088  路  3Comments

seanmcilvenna picture seanmcilvenna  路  3Comments

Sankrish picture Sankrish  路  4Comments