XMLHttpRequest is not defined - Papa parse

2019-09-12 07:07发布

问题:

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 :)

回答1:

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.