I am working on a DataConnector class that is loading a JSON file. Everything outside of this class is working perfectly fine in my app, so I don't think the issue is arrising anywhere else.
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
//#File: DataConnector.js
var DataConnector = {};
DataConnector.LoadJSON = (function(file,callback) {
var xobj = new XMLHttpRequest();
console.log(xobj);
xobj.open('GET', file, true);
xobj.overrideMimeType = "application/json";
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
callback(xobj.responseText);
}
};
xobj.send(null);
})
DataConnector.getMessage = function(){
return 'Hello DataConnector';
}
module.exports = DataConnector;
//#File: QuestionJS.js
//var $ = require('jquery');
Answer = require('./Answer');
Question = require('./Question');
ThemeGenerator = require('./ThemeGenerator');
DataConnector = require('./DataConnector')
//console.log(DataConnector.getMessage());
If I comment DataConnector out from being required, webpack is able to bundle my app perfectly fine. I was also working on this earlier today on a different PC and it actualy worked perfectly fine every time.
I have used npm i -g xmlhttprequest npm i xmlhttprequest npm i xmlhttprequest --save
xmlhttprequest has files in the node_modules folder in my project folder and I am following the readme precisely.
I have honestly never been more confused, especially since this error never even showed up on the other system despite both being windows 10 systems with the save software installed.
Thanks for any help guys!
The xmlhttprequest
package is meant for server-side JavaScript. The child_process
package is built into NodeJS. It is not an NPM package, so cannot be downloaded or included in any application outside of running on NodeJS. It is a package designed specifically for handling functionality internal to NodeJS.
It appears if you are trying to use the xmlhttprequest
package on the front end. Since the xmlhttprequest
package only works on NodeJS, you are receiving the error that child_process
does not exist, because it does not exist in a front end browser. If that is the case, you may be pleased to know that XMLHttpRequest is built into browsers. The reason the NodeJS package exists is to mimic the browser's innate functionality. You may be able to just remove the package as a whole and refer to the XMLHttpRequest
class without error.
If you are working on code to support both back end and front end, you may result to something like this:
if (typeof XMLHttpRequest === 'undefined') {
global.XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
}
If you are using Webpack to bundle a back end application, you may need to configure it to target node
instead of a browser.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.