DiegoZoracKy / convert-excel-to-json

Convert Excel to JSON, mapping sheet columns to object keys.
MIT License
290 stars 92 forks source link

_fs.readFileSync is not a function #44

Open panavsethi1 opened 4 years ago

panavsethi1 commented 4 years ago

This shows up while following the simple steps in npm documentation

DiegoZoracKy commented 4 years ago

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

HenrikEilers commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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!

BluebambooSRL commented 3 years ago

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