问题描述
我正在尝试开始使用 Electron.我已经能够运行所有 简单示例.它们都按预期工作.当我尝试遵循 快速入门指南时,我遇到了与中提到的相同的问题这个问题:应用启动正常,但不显示节点 Chrome 和 Electron 的版本.当我查看开发工具时,我看到了这个错误:
I am trying to get started with Electron. I was already able to run all simple examples. They all work as expected. When I try to follow the Quick Start Guide I experience the same issue as mentioned in this question: The app launches properly, but does not display the versions of node Chrome and Electron. When I look into the developing tools I see this error:
Uncaught ReferenceError: process is not defined
at index.html:14
但是,我 将 nodeIntegration
设置为 true
.
However, I have set nodeIntegration
to true
.
为什么还是不行?
版本:
- npm 7.6.3
- 节点 v14.16.0
- 铬 89.0.4389.82
操作系统:
- Ubuntu 20.04.2 LTS.
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>
<body style="background: white;">
<h1>Hello World!</h1>
<p>
We are using node
<script>document.write(process.versions.node)</script>,
Chrome
<script>document.write(process.versions.chrome)</script>,
and Electron
<script>document.write(process.versions.electron)</script>.
</p>
</body>
</html>
main.js
const { app, BrowserWindow } = require('electron')
function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
win.loadFile('index.html')
}
app.whenReady().then(createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
package.json
{
"name": "my-electron-app",
"version": "0.1.0",
"author": "username",
"description": "My Electron app",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"devDependencies": {
"electron": "^12.0.0"
}
}
推荐答案
尝试在创建 BrowserWindow
时设置此值:
Try to set this value when creating your BrowserWindow
:
webPreferences: { nodeIntegration: true, contextIsolation: false }
一个新的主要 Electron 版本已经发布,它打破了教程.
具体的重大更改是 contextIsolation
标志的新默认值.
A new major Electron version has been released which broke the tutorial.
The specific breaking change is a new default value of contextIsolation
flag.
有关详细信息,请参阅此 GitHub 问题.
For more details, see this GitHub issue.
这篇关于没有为 Electron 的入门应用程序定义流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!