【问题标题】:using npm module in renderer process of electron在电子的渲染器进程中使用 npm 模块
【发布时间】:2021-07-20 12:00:12
【问题描述】:

我已经在我的机器上通过npm install Buffer 安装了 Buffer 模块,我想简单地将它导入渲染器进程以使用 Buffer

当我使用这个时:

const Buffer = require('Buffer')

它说 require 是未定义的。

Stack Overflow 上的解决方案都不起作用。

【问题讨论】:

标签: javascript electron


【解决方案1】:

确保您将BrowserWindow 设置中的nodeIntegration 设置为true,并将contextIsolation 设置为false,如下所示:

new BrowserWindow({
    webPreferences:  {
        nodeIntegration:  true,
        contextIsolation: false
    },
});

默认情况下,nodeIntegrationfalse,这会阻止您在渲染器进程中使用 NPM 模块,打开 nodeIntegration 将解决此问题。

Read more here

注意:要从 Renderer 进程访问 Node.js API,您需要将 nodeIntegration 首选项设置为 true,并将 contextIsolation 首选项设置为 false。

免责声明,打开 nodeIntegration 会在您的应用中打开安全漏洞。 See Zac's answer 了解如何修复它们。

【讨论】:

  • 此答案在您的应用中打开 security vulnerabilities
  • @Zac 非常正确,您对这些安全漏洞的解释非常全面,我很感激。
猜你喜欢
  • 1970-01-01
  • 2015-12-28
  • 2019-04-11
  • 2017-04-03
  • 2016-11-26
  • 2020-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多