node.js - Electron.js快速入门指南: Errors on “npm start”

标签 node.js npm electron npm-scripts 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
似乎在index.html中未定义process。但是,当我直接运行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
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.

关于node.js - Electron.js快速入门指南: Errors on “npm start” ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66516677/

相关文章:

node.js - 如何升级 NodeJS Docker 容器?

node.js - Flipkart 附属客户端 Node : 404 not found on product search

javascript - 使用pdfjs时,无法通过在viewer.html文件参数中传递Uint8Array来呈现pdf

mysql - Sequelize.js:如何处理与 MySQL 的重新连接

node.js - 如何在mongodb中查询嵌套的对象数组?

javascript - 在 vanilla JavaScript 中向服务器发送 GET 请求的最佳方式是什么?

javascript - jQuery 查找元素 :contains, 然后获取类

node.js - 使用 Postgresql order by 首先使用空值进行 Sequelize

node.js - NPM 全局包未满足依赖关系

typescript - 如何使用 Electron 锻造和 Electron 打包器缩小 Electron 应用程序中的 typescript 源代码?