Electron.js 快速入门指南:"npm start" 上的错误

Electron.js quick start guide: Errors on "npm start"

我是 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

似乎 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

确保将 nodeIntegration 设置为 true。

main.js

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

您应该同时使用 contextIsolation: falsenodeIntegration: true

尝试以下操作:

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

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

What is context Isolation?

Context Isolation is a feature that ensures that both your preload scripts and Electron's internal logic run in a separate context to the website you load in a webContents. This is important for security purposes as it helps prevent the website from accessing Electron internals or the powerful APIs your preload script has access to.

This means that the window object that your preload script has access to is actually a different object than the website would have access to. For example, if you set window.hello = 'wave' in your preload script and context isolation is enabled window.hello will be undefined if the website tries to access it.