javascript 电子 5.0.0 “未捕获的 ReferenceError:需要未定义”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/55093700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:29:34  来源:igfitidea点击:

electron 5.0.0 "Uncaught ReferenceError: require is not defined"

javascriptnode.jselectronrequire

提问by junvar

I had initially been using electron stable (4.x.x), and was able to use requirein both my browser and renderer processes. I upgraded to electron beta (5.0.0) because I needed a newer version of node and encountered this error message in my renderer process, Uncaught ReferenceError: require is not defined.

我最初使用的是 Electron stable (4.xx),并且能够require在我的浏览器和渲染器进程中使用。我升级到电子测试版(5.0.0),因为我需要更新版本的节点并在我的渲染器进程中遇到此错误消息,Uncaught ReferenceError: require is not defined.

Googling and looking through the electron docs, I found comments saying the error could be caused by setting webPreferences.nodeIntegrationto false when initializing the BrowserWindow; e.g.: new BrowserWindow({width, height, webPreferences: {nodeIntegration: false}});. But I was not doing this, so I thought something else must be the issue and continued searching for a resolution.

谷歌搜索并查看电子文档,我发现评论说错误可能是由于webPreferences.nodeIntegration在初始化时设置为 false引起的BrowserWindow;例如:new BrowserWindow({width, height, webPreferences: {nodeIntegration: false}});。但我没有这样做,所以我认为一定是其他问题,并继续寻找解决方案。

回答by junvar

It turns out, nodeIntegrationwas true by default in previous electron versions, but false by default in 5.0.0. Consequently, setting it to true resolved my issue. Not finding this change documented online in comments or on electrons page, I thought I'd make this self-answered SO post to make it easier to find for future people who encounter this issue.

事实证明,nodeIntegration在以前的电子版本中默认为真,但在 5.0.0 中默认为假。因此,将其设置为 true 解决了我的问题。没有在评论或电子页面中找到在线记录此更改,我想我会制作这个自我回答的 SO 帖子,以便将来遇到此问题的人更容易找到。

回答by Stev

Like junvarsaid, nodeIntegrationis now false by default in 5.0.0.

就像junvar所说的,nodeIntegration现在在 5.0.0 中默认为 false。

The electronjs FAQhas some sample code on how to set this value.

electronjs FAQ有关于如何设置这个值一些示例代码。

let win = new BrowserWindow({
  webPreferences: {
    nodeIntegration: true
  }
})
win.show()

回答by Sri Darshana

set nodeIntegration to true when creating new browser window.

创建新的浏览器窗口时将 nodeIntegration 设置为 true。

app.on('ready', () => {
    mainWindow = new BrowserWindow({
        webPreferences: {
            nodeIntegration: true
        }
    });
});

回答by dspring

junvaris right, nodeIntegrationis false by default in v5.0.0.

junvar是对的,nodeIntegration在 v5.0.0 中默认为 false。

It's the last statement in the Other Changessection of Release Notes for v5.0.0and was also mentioned in this PR

这是v5.0.0Other Changes发行说明部分中的最后一条语句,在此 PR 中也有提及

回答by ShapCyber

Readers of this post should read the Do not enable Node.js Integration for Remote Contentsection from the Security, Native Capabilities, and Your ResponsibilityGuidebefore making a decision.

这篇文章的读者应该在做出决定之前阅读安全性、本机功能和您的责任指南中不要为远程内容启用 Node.js 集成部分。

// Bad
const mainWindow = new BrowserWindow({
  webPreferences: {
    nodeIntegration: true,
    nodeIntegrationInWorker: true
  }
})
mainWindow.loadURL('https://example.com')

// Good
const mainWindow = new BrowserWindow({
  webPreferences: {
    preload: path.join(app.getAppPath(), 'preload.js')
  }
})
mainWindow.loadURL('https://example.com')