question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

_fs.readFileSync is not a function

See original GitHub issue

Having difficulty reading XLSX files in the browser.

  1. npm install --save xlsx
  2. Ensure and xlsx folder is present in /node_modules
  3. In my JS file, add the following:
import XLSX  from `xlsx`;
var workbook = XLSX.readFile('../../data/test.xlsx');

Immediately hit the error:

xlsx.js:11387 Uncaught TypeError: _fs.readFileSync is not a function

And yet if I transition the import statement to something more ES5 friendly (require), the application can’t even locate the XLSX library anymore, which is confusing because it’s in /node_modules:

if(typeof require !== 'undefined') XLSX = require('xlsx');
var workbook = XLSX.readFile('../../data/test.xlsx');

Immediately hit the error:

my-test-file.jsx:55 Uncaught ReferenceError: XLSX is not defined

I added the following to my webpack per some previous open issues, however I didn’t see a decent explanation for why this is required:

resolve: {
    extensions: ['', '.js', '.jsx', '.json']
},
node: {
    fs: 'empty'
},

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
mikechabotcommented, May 27, 2016

Yeah, this code is getting hit in the browser, so access to the filesystem is a no-go. I had hoped webpack might work some magic and stuff the contents in bundle.js. That said, would need a webpack loader for .xls* to even get at it, but if the dependency is still on fs then it might be a moot point. Alternative is to just spin up a simple Express server and move the work over there.

2reactions
arirawrcommented, May 27, 2016

So I’m thinking fs just doesn’t work with client side JS (and thus with webpack): https://github.com/webpack/webpack/issues/411

Read more comments on GitHub >

github_iconTop Results From Across the Web

readFileSync is not a function - Stack Overflow
I am relatively new to Node. js and have been looking around but cannot find a solution. I did check the require javascript...
Read more >
Uncaught TypeError: fs.readFileSync is not a function #10551
Hi, I am facing issue in node js file system plugin when I run the web pack it's showing me an error. I...
Read more >
Node.js fs.readFileSync() Method - GeeksforGeeks
readFileSync () method is an inbuilt application programming interface of fs module which is used to read the file and return its content....
Read more >
Do not use fs sync methods in Javascript, use fs.promises ...
Synchronous functions don't play well with Javascipt's single thread. Fortunately, there is an alternative.
Read more >
File system | Node.js v19.3.0 Documentation
readFileSync (path[, options]); fs. ... The node:fs module enables interacting with the file system in a way modeled on standard POSIX functions.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found