我是 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: false
和nodeIntegration: true
请尝试以下方法:
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
})
这是在这里讨论:https://github.com/electron/electron/issues/18139What 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/