没有为 Electron 的入门应用程序定义流程

Process is not defined for Electron's Getting Started App

我正在尝试开始使用 Electron。我已经能够 运行 所有 simple examples. They all work as expected. When I try to follow the Quick Start Guide I experience the same issue as mentioned in :应用程序正常启动,但不显示节点 Chrome 和 Electron 的版本。当我查看开发工具时,我看到了这个错误:

Uncaught ReferenceError: process is not defined
    at index.html:14

但是,我已经nodeIntegration设置为true

为什么还是不行?

版本:

操作系统:

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 时设置此值:

webPreferences: { nodeIntegration: true, contextIsolation: false }

一个新的major Electron version has been released打破了教程。
具体的重大更改是 contextIsolation 标志的新默认值。

有关详细信息,请参阅 this GitHub issue