简体   繁体   English

无法解析模块xmlhttprequest中的“ child_process”

[英]Can't Resolve “child_process” in module xmlhttprequest

I am working on a DataConnector class that is loading a JSON file. 我正在处理正在加载JSON文件的DataConnector类。 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;

在此处输入图片说明 main.js main.js

//#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. 如果我不再需要DataConnector,则webpack可以很好地捆绑我的应用程序。 I was also working on this earlier today on a different PC and it actualy worked perfectly fine every time. 我今天早些时候还在另一台PC上进行这项工作,实际上每次都运行得很好。

I have used npm i -g xmlhttprequest npm i xmlhttprequest npm i xmlhttprequest --save 我已经使用过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. xmlhttprequest在我的项目文件夹的node_modules文件夹中有文件,我正在严格遵循自述文件。

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. 老实说,我从未感到困惑,特别是因为即使这两个都是安装了保存软件的Windows 10系统,该错误甚至都没有出现在其他系统上。

Thanks for any help guys! 感谢您的帮助!

The xmlhttprequest package is meant for server-side JavaScript. xmlhttprequest软件包用于服务器端 JavaScript。 The child_process package is built into NodeJS. child_process软件包内置在child_process It is not an NPM package, so cannot be downloaded or included in any application outside of running on NodeJS. 它不是NPM软件包,因此不能在NodeJS上运行之外下载或包含在任何应用程序中。 It is a package designed specifically for handling functionality internal to NodeJS. 它是专门设计用于处理NodeJS内部功能的软件包。

It appears if you are trying to use the xmlhttprequest package on the front end. 如果您尝试在前端使用xmlhttprequest包,则会出现该xmlhttprequest 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. 由于xmlhttprequest包仅适用于xmlhttprequest ,因此您将收到child_process不存在的错误,因为前端浏览器中不存在child_process If that is the case, you may be pleased to know that XMLHttpRequest is built into browsers. 如果是这样,您可能会很高兴知道XMLHttpRequest内置在浏览器中。 The reason the NodeJS package exists is to mimic the browser's innate functionality. 存在NodeJS软件包的原因是为了模仿浏览器的固有功能。 You may be able to just remove the package as a whole and refer to the XMLHttpRequest class without error. 您也许可以整体上删除该程序包并引用XMLHttpRequest类,而不会出现错误。

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. 如果使用Webpack捆绑后端应用程序,则可能需要将其配置为目标node而不是浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未找到模块:无法解析“child_process”NextJS + Nightmare - Module not found: Can't resolve 'child_process' NextJS + Nightmare 找不到模块:使用 Electron React Boilerplate 时无法解析“child_process” - Module not found: Can't resolve 'child_process' while using Electron React Boilerplate 找不到模块:无法解析“child_process” - google-spreadsheet - Module not found: Can't resolve 'child_process' - google-spreadsheet 在 Next.js / Webpack 5 中安装 Plaiceholder 导致:找不到模块:无法解析“child_process” - Installing Plaiceholder in Next.js / Webpack 5 causes: Module not found: Can't resolve 'child_process' 错误:无法解析“child_process”和错误:无法解析“fs” - Error: Can't resolve 'child_process' and Error: Can't resolve 'fs' InternalError Metro 遇到错误:尝试解析模块“child_process”时 - InternalError Metro has encountered an error: While trying to resolve module `child_process` 在Webpack中的/ node_modules / watchpack中获取“无法解析模块'aws-sdk','child_process','net'” - Getting “Cannot resolve module 'aws-sdk','child_process','net'” in /node_modules/watchpack in Webpack 使用没有Webpack的模块“child_process” - Using module “child_process” without Webpack 反应原生中的未知模块“child_process” - unknown module “child_process” in react native 在node.js的child_process模块​​中 - In the child_process module in node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM