我是 Electron 的新手,并遵循了此处的快速入门指南:https://www.electronjs.org/docs/tutorial/quick-start
我的 package.json
有这个:
"scripts": {
"start": "electron ."
}
当我运行 npm start
时,应用程序启动但未打印版本,并且我在 js 控制台中收到这些错误:
Uncaught ReferenceError: process is not defined at index.html:11
Uncaught ReferenceError: process is not defined at index.html:12
Uncaught ReferenceError: process is not defined at index.html:13
似乎在 index.html 中没有定义 process
。但是当我直接运行 electron .
时,一切正常。
为什么?
我的版本:
<块引用>Manjaro 20.2.1,内核 5.10.18-1-MANJARO
Node.js 15.10.0
npm 7.6.1
电子12.0.0
答案 0 :(得分:2)
您应该将 contextIsolation: false
与 nodeIntegration: true
一起使用
尝试以下操作:
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
})
这里正在讨论这个:https://github.com/electron/electron/issues/18139
<块引用>上下文隔离是一项功能,可确保您的预加载 脚本和 Electron 的内部逻辑在单独的上下文中运行 您在 webContents 中加载的网站。这对安全很重要 目的,因为它有助于防止网站访问 Electron 内部或您的预加载脚本可以访问的强大 API。
这意味着您的预加载脚本有权访问的窗口对象 to 实际上是一个不同于网站可以访问的对象 到。例如,如果您在预加载中设置 window.hello = 'wave' 启用脚本和上下文隔离 window.hello 将是未定义的 如果网站尝试访问它。
答案 1 :(得分:0)
确保您已将 nodeIntegration
设置为 true。
main.js
const { BrowserWindow } = require('electron')
let win = new BrowserWindow({
webPreferences: {
nodeIntegration: true
}
})