XMLHttpRequest is not defined - Papa parse
Asked Answered
I

1

3

EDIT: [SOLVED] For anyone who reads this, Papa Parse is a parser for the browser, not Node.js. Baby Parse is used for Node.js, but know that it isn't as extensive in its functionality and can't parse straight from a file, only from a string.

In the papaparse.js file, it has this line of code:

xhr = new XMLHttpRequest();

This seems to be causing the error and I'm not sure how to get around it.

My code:

var Papa = require('papaparse');
var data;

process.argv.forEach(function(val, index, array) {
    console.log(index + ': ' + val);
});
var file = process.argv[2];
console.log("File: " + file);


// Parse the file
Papa.parse(file, {
    header: true,
    download: true,
    dynamicTyping: true,
    complete: function(results) {
        data = results;
        console.log(data);
    }
});

Any ideas would be appreciated :)

Integral answered 28/7, 2016 at 18:24 Comment(3)
Sounds like you're trying to run browser Javascript in Node.js. You can't do that.Acclimatize
you could also use babyparse that is commonjs github.com/Rich-Harris/BabyParseEsthonia
Yup, you're absolutely right. Papa parse is for the browser, and I just found out Baby Parse is for node. I see the issue now, thanks!Integral
C
5

actually PapaParse can be used in a node environment now: https://github.com/mholt/PapaParse/blob/master/README.md#papa-parse-for-node

however, you have to disable the "download" option in the parse-config, since otherwise the library will attempt to load the file via XMLHttpRequest.

Carny answered 5/2, 2019 at 14:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.