convert-excel-to-json icon indicating copy to clipboard operation
convert-excel-to-json copied to clipboard

_fs.readFileSync is not a function

Open panavsethi1 opened this issue 4 years ago • 6 comments

This shows up while following the simple steps in npm documentation

panavsethi1 avatar Sep 07 '20 18:09 panavsethi1

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

DiegoZoracKy avatar Sep 07 '20 22:09 DiegoZoracKy

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: https://github.com/SheetJS/sheetjs/issues/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 avatar Sep 08 '20 16:09 HenrikEilers

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
            });

HenrikEilers avatar Sep 08 '20 18:09 HenrikEilers

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}}' }
            })
          })
      }```

cnjmike avatar Sep 24 '20 18:09 cnjmike

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!

dwivedithedev avatar Nov 27 '20 14:11 dwivedithedev

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

BluebambooSRL avatar Jul 06 '21 16:07 BluebambooSRL