如何导入同名的节点模块?

时间:2019-11-20 03:48:13

标签: node.js reactjs electron require

这是用于电子应用程序。我正在尝试导入nodejs http模块,但是执行require('http')似乎是在加载stream-http模块。使用它的地方可能存在依赖关系。我基本上是想在电子应用中执行http.createServer()。该应用程序是这样设置的...

有一个BrowserWindow,其属性为:webPreferences: { nodeIntegration: true, webSecurity: false, }。此BrowserWindow渲染器具有react组件,该语句在构造函数中的某个位置:console.log(require('http')); 输出看起来像这样:

{request: ƒ, get: ƒ, ClientRequest: ƒ, IncomingMessage: ƒ, Agent: ƒ, …}
Agent: ƒ ()
ClientRequest: ƒ (opts)
IncomingMessage: ƒ (xhr, response, mode, fetchTimer)
METHODS: (26) ["CHECKOUT", "CONNECT", "COPY", "DELETE", "GET", "HEAD", "LOCK", ...
get: ƒ (opts, cb)
globalAgent: push../node_modules/stream-http/index.js.http.Agent {}
request: ƒ (opts, cb)
__proto__: Object

基本上,这正在加载http-stream模块。如何显式加载原始的nodejs http模块?

奖金问题:在开发人员工具窗口中进行require('http')给了我原始的nodejs http模块。为什么会这样?

1 个答案:

答案 0 :(得分:1)

您是如何生成应用程序的?在哪里需要“ http”模块?

请尝试根据本指南https://electronjs.org/docs/tutorial/first-app#trying-this-example设置您的应用程序。

在那之后转到您的main.js文件,最重要的是

const server = require("http")
console.log(server)

我尝试了上述操作,并正确获取了http服务器模块

stream-http是浏览器的Node本机http模块的实现。它试图尽可能匹配Node的API和行为,但是某些功能不可用,因为浏览器对请求的控制不多。如果您误安装了它,请检查package.json并尝试将其删除。