Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_fs.readFileSync is not a function #44

Open
panavsethi1 opened this issue Sep 7, 2020 · 6 comments
Open

_fs.readFileSync is not a function #44

panavsethi1 opened this issue Sep 7, 2020 · 6 comments

Comments

@panavsethi1
Copy link

This shows up while following the simple steps in npm documentation

@DiegoZoracKy
Copy link
Owner

Can you send the entire error log along with the Node.js version you are using?

@HenrikEilers
Copy link

I have the same Problem. The reason seems to be that clientside js cant use the fs(FileSystem).

Here is the same issue with another package:
SheetJS/sheetjs#418

According to the Answers there, the Problem can be solved by using a html Input tag and reading the file that way. But i have not tried it yet.

I hope that helps.

@HenrikEilers
Copy link

It works! But the code has to be modified. I added a posibiliy to pass a "type" argument to the underlying xlsx package.

My code looks like this:

const onChange = (e) => {
    const reader = new FileReader();
    reader.onloadend = function (event) {
      console.log(event.target.result);
      const tmp = excelToJson({ source: event.target.result });
      console.log(tmp);
    };
    reader.readAsArrayBuffer(e.target.files[0]);
  };

<input type="file" onChange={onChange} />

and the change in convert-excel-to-json.js looks like this:

if (_config.source) {
            workbook = XLSX.read(_config.source, {
                type:"buffer", 
                sheetStubs: true,
                cellDates: true
            });

@cnjmike
Copy link

cnjmike commented Sep 24, 2020

Final edit:

Henrik's change in convert-excel-to-json.js also works for me, with a slight variation on where the array buffer comes from:

with this.file coming from a standard HTML file picker:

        this.file.arrayBuffer()
          .then(buffer => {
            const batchJson = excelToJson({
              source: buffer,
              header: { rows: 1 },
              columnToKey: { '*': '{{columnHeader}}' }
            })
          })
      }```

@dwivedithedev
Copy link

It works! But the code has to be modified. I added a posibiliy to pass a "type" argument to the underlying xlsx package.

My code looks like this:

const onChange = (e) => {
    const reader = new FileReader();
    reader.onloadend = function (event) {
      console.log(event.target.result);
      const tmp = excelToJson({ source: event.target.result });
      console.log(tmp);
    };
    reader.readAsArrayBuffer(e.target.files[0]);
  };

<input type="file" onChange={onChange} />

and the change in convert-excel-to-json.js looks like this:

if (_config.source) {
            workbook = XLSX.read(_config.source, {
                type:"buffer", 
                sheetStubs: true,
                cellDates: true
            });

This worked for me as well. Thank you!

Domyy95 added a commit to Domyy95/convert-excel-to-json that referenced this issue May 17, 2021
@BluebambooSRL
Copy link

Works perfect!!! @DiegoZoracKy Could commit the change pls?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants