_fs.readFileSync is not a function
See original GitHub issueHaving difficulty reading XLSX files in the browser.
npm install --save xlsx
- Ensure and
xlsx
folder is present in/node_modules
- 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:
- Created 7 years ago
- Comments:7
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.So I’m thinking
fs
just doesn’t work with client side JS (and thus with webpack): https://github.com/webpack/webpack/issues/411