简体   繁体   English

电子:NodeJS 'net' 模块返回空对象

[英]electron: NodeJS 'net' module returns empty object

I just started to work on my first electron app and imported some existing code that uses the net module.我刚刚开始开发我的第一个电子应用程序并导入了一些使用net模块的现有代码。 When I import the code from the electron app, the net module returns an empty object.当我从电子应用程序导入代码时, net模块返回一个空对象。

const net = require('net'); // => {}

I thought that electron bundles node together with the required native modules, so I'm a bit confused about why the import isn't working.我认为电子将节点与所需的本机模块捆绑在一起,所以我对导入不起作用的原因感到有些困惑。

EDIT: I've bootstrapped the app with vue-cli-tools , so it's using webpack to bundle dependencies.编辑:我已经使用vue-cli-tools引导了该应用程序,因此它使用 webpack 来捆绑依赖项。 Not sure if this is important.不确定这是否重要。

I thought that electron bundles node together with the required native modules,我认为电子将节点与所需的本机模块捆绑在一起,

It does, but having a browser runtime and a node.js runtime bundled together still leaves you with two different runtimes.确实如此,但是将浏览器运行时和 node.js 运行时捆绑在一起仍然会给您留下两个不同的运行时。

Understanding the difference between the Main and Renderer processes is essential for Electron development.了解Main 和 Renderer 进程之间的区别对于 Electron 开发至关重要。

The Main process runs on the Node.js runtime and has access to Node.js APIs (like net ). Main 进程在 Node.js 运行时上运行,并且可以访问 Node.js API(如net )。

The Renderer process runs on Chromium and has access to browser APIs (like the DOM). Renderer 进程在 Chromium 上运行,并且可以访问浏览器 API(如 DOM)。

You can't access net from your client-side Vue code because it is running in the Renderer process.您无法从客户端 Vue 代码访问net ,因为它在 Renderer 进程中运行。

You need to create a service in the Main process and exchange data between it and something running in the Renderer.您需要在 Main 进程中创建一个服务,并在它与在 Renderer 中运行的东西之间交换数据。

This is typically done using the IPC apis .这通常是使用IPC apis完成的。

// In main process.
const { ipcMain } = require('electron')
ipcMain.on('message-from-browser', (event, arg) => {
  const something = doSomethingWithNetModule()
  event.reply('reply-to-browser', something)
})

and

// In renderer process (web page).
const { ipcRenderer } = require('electron')

ipcRenderer.on('asynchronous-reply', (event, arg) => {
    doSomethingWithDataFromNetModule(arg)
})

ipcRenderer.send('message-from-browser', 'do something with the net module please')

Note that since you are using Webpack you will have issues with require("electron") which are explained in this question .请注意,由于您使用的是 Webpack,您将遇到require("electron") 问题,这在此问题中进行了解释。

Weback may be trying to bundle net . Weback 可能正在尝试捆绑net Two things to try:尝试两件事:

In vue.config.js configure the externals option with webpack.vue.config.js配置 externals 选项。 (note you may neet commonjs2 in the option below, see the externals documentation for more info) (请注意,您可能需要在下面的选项中使用 commonjs2,有关更多信息,请参阅外部文档)

module.exports = {
  configureWebpack: {
    externals: {
      net: 'commonjs net',
    }
  },
}

Alternatively (and less recommended, but has worked in the past when finding the right webpack settings has proved stubborn), rename require so that webpack ignores it:或者(不太推荐,但过去在找到正确的 webpack 设置时已经证明是顽固的),重命名 require 以便 webpack 忽略它:

const _require = require;
//...
const net = _require('net');

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM