PapaParse icon indicating copy to clipboard operation
PapaParse copied to clipboard

ReferenceError: FileReaderSync is not defined on the backend (NodeJS)

Open skalyanapu-mtuity opened this issue 6 years ago • 19 comments

Hi,

I'm using Papaparse on the backend using NodeJS, I'm facing some strange error something related firefox while parsing on the backend. Can you please suggest what was wrong ??

Using Postman to trigger POST request and passing the file as a multipart data to API. Attached screenshot reference. pp

skalyanapu-mtuity avatar Mar 20 '18 17:03 skalyanapu-mtuity

are you using worker mode on server side?

pokoli avatar Mar 21 '18 09:03 pokoli

Encountering the same error, but worker in params is at 'false'. Changing its value still give the same error.

Is there something to configure for the backend parsing ?

ReinargJ avatar Apr 02 '18 08:04 ReinargJ

Same error here:

const inStream = AWS.S3.getObject({ ... }).createReadStream()

      Papa.parse(inStream, {
        header: true,
        worker: false,
        step: (...args) => {
          console.log(args)
        },
        complete: (...args) => {
          console.log('done',args)
        }
      })
ReferenceError: FileReaderSync is not defined
    at FileStreamer.stream (.../node_modules/papaparse/papaparse.js:681:5)
    at Object.CsvToJson [as parse] (.../node_modules/papaparse/papaparse.js:244:19)
    ...

shanonvl avatar Apr 05 '18 20:04 shanonvl

@pokoli I tried with worker and without worker mode also but no luck, any suggestions ??

skalyanapu-mtuity avatar Apr 09 '18 04:04 skalyanapu-mtuity

It seems that PapaParse is not detecting your stream as a readable so it uses the FileAPI from browser which is not available on Node. This condition is failing:

https://github.com/mholt/PapaParse/blob/master/papaparse.js#L237

I think we should correctly detect the stream you are passing.

Can somebody upload a testing scenario, so I can reproduce it?

pokoli avatar Apr 09 '18 08:04 pokoli

I am getting this issue within electron 1.8.2

damiandennis avatar May 09 '18 21:05 damiandennis

actually it was an issue I was doing, I was meant to use unparse not parse and that caused the exception.

damiandennis avatar May 09 '18 21:05 damiandennis

Having the same problem, but it doesn't seem to be happening on every PC. On my computer with Linux it works both on chrome and firefox. On my co-workers Windows PC, it throws the error

lucapasquale avatar May 28 '18 13:05 lucapasquale

So this may be something related to Windows only. Sorry but as I linux user I can not reproduce.

Did you know if it's specific to some node versions?

Can somebody having the issue post the Node version that they are using?

pokoli avatar May 28 '18 14:05 pokoli

ole@mki:~/papa$ node --version v9.11.1

I'm also having this issue on Ubuntu 17.10. I'm using this csv file for the test. Here's the code I'm running:

const papa = require('papaparse');
const fs = require('fs');
const c = fs.readFileSync('sample.csv');

const d = papa.parse(c);

Stacktrace:

ReferenceError: FileReaderSync is not defined
    at FileStreamer.stream (/home/ole/papa/node_modules/papaparse/papaparse.js:681:5)
    at Object.CsvToJson [as parse] (/home/ole/papa/node_modules/papaparse/papaparse.js:245:19)
    at Object.<anonymous> (/home/ole/papa/index.js:6:16)
    at Module._compile (internal/modules/cjs/loader.js:654:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
    at Module.load (internal/modules/cjs/loader.js:566:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
    at Function.Module._load (internal/modules/cjs/loader.js:498:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:695:10)
    at startup (internal/bootstrap/node.js:201:19)

oleersoy avatar Jun 04 '18 02:06 oleersoy

This fixes it for me (Just add utf8 when reading the file:

const c = fs.readFileSync('sample.csv', 'utf8');

oleersoy avatar Jun 04 '18 15:06 oleersoy

if you don't add the 'utf8' to the fs.readFileSync, the return value is node Buffer which confuses papaparse.

amit777 avatar Jun 28 '18 17:06 amit777

Interesting - I feel like we should add support to handle a Node Buffer

dboskovic avatar Jul 31 '18 22:07 dboskovic

@dboskovic on #370 we added support for Node Streams, which for me seems equivalent as a Node Buffer

pokoli avatar Aug 01 '18 07:08 pokoli

I have the same issue when saving a csv file in Notepad++ and saving it through WinSCP to the server.

giorgio79 avatar Aug 18 '18 06:08 giorgio79

Just discovered this lib, and came across the same issue while attempting to parse a CSV stored in an S3 bucket. Upon console logging the returned S3 getObject response object, the appropriate data is returned in a Node Buffer. Per the suggestions earlier (and following a few related SO suggestions like this one: https://stackoverflow.com/questions/36942442/how-to-get-response-from-s3-getobject-in-node-js), I've tried reformatting with .toString() and .toString('utf8') before feeding into Papa, but no luck.

Here's a snippet of what I'm attempting to do:

// initialize empty dict to store parsed returns
let info_obj;
// gen S3 params
let params = {
        Bucket: ORIGIN_BUCKET,
        Key: srcKey
    };
info_obj = Papa.parse(s3.getObject(params, function(err, data) {
    if (err) {
        console.log(err, err.stack);
    } else {
        console.log(data);
        return data.Body.toString();  // this has been tested adding 'utf8' as well
    }
}), {
    header: true
});

I've tried disconnecting the getObject() call from the parse, as well, just to make sure I'm not overlooking something obvious, but I'm new to JS. Any ideas?

humanatwork avatar Aug 24 '18 18:08 humanatwork

@PresBAW have a look at #494 and please open a new issue if the topic is not related

pokoli avatar Aug 27 '18 07:08 pokoli

Following the instructions to run the test suite I also get the same error in 8 tests

SFUMisha avatar Oct 25 '23 23:10 SFUMisha

As a workaround on nodejs you can read the text content of the file itself and use the text instead of the file descriptor API.

RenaudF avatar Jan 31 '24 11:01 RenaudF