rese
rese

Reputation: 63

How to import node module with same name?

This is for an electron app. I'm trying to import the nodejs http module, but doing require('http') seems to load the stream-http module instead. There is probably a dependency somewhere using it. I'm basically trying to do http.createServer() in the electron app. The app is set up like this...

There is a BrowserWindow with the property: webPreferences: { nodeIntegration: true, webSecurity: false, }. This BrowserWindow renderer has a react component, and this statement somewhere in the constructor: console.log(require('http')); The output looks like this:

{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

Basically this is loading the http-stream module. How can I explicitly load the original nodejs http module?

Bonus question: doing require('http') in the developer tools window gives me the original nodejs http module. Why is that?

Upvotes: 1

Views: 867

Answers (1)

Konstanitnos Schoinas
Konstanitnos Schoinas

Reputation: 159

How did you generate your application? And where do you require your "http" module?

Please try to setup your application from this guide https://electronjs.org/docs/tutorial/first-app#trying-this-example.

After that go to your main.js file and on top do

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

I tried the above and got the http server module properly

The stream-http is an implementation of Node's native http module for the browser. It tries to match Node's API and behavior as closely as possible, but some features aren't available, since browsers don't give nearly as much control over requests. If you have installed it by mistake check your package.json and try to remove it.

Upvotes: 1

Related Questions