electronic .js快速启动指南:"npm start"



我是电子新手,遵循快速入门指南: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

似乎process没有在index.html中定义。但是当我直接运行electron .时,一切都正常工作。

为什么?

我的版本:

Manjaro 20.2.1, Kernel 5.10.18-1-MANJARO
Node.js 15.10.0
npm 7.6.1
electron 12.0.0

您应该使用contextIsolation: falsenodeIntegration: true

尝试以下操作:

const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
})

这里正在讨论:https://github.com/electron/electron/issues/18139

什么是上下文隔离?

上下文隔离是一项功能,可确保您的预加载脚本和Electron的内部逻辑在单独的上下文中运行你在webContents中加载的网站。这对安全很重要目的,因为它有助于防止网站访问电子

这意味着预加载脚本可以访问的窗口对象To实际上是一个不同于网站可以访问的对象出现。例如,如果您设置window。Hello = 'wave'在你的预加载启用脚本和上下文隔离窗口。Hello是未定义的如果网站试图访问它。

确保nodeIntegration设置为true

main.js

const { BrowserWindow } = require('electron')
let win = new BrowserWindow({
webPreferences: {
nodeIntegration: true
}
})

最新更新