运行“First Electron App"不会显示版本? [英] Running the "First Electron App" wont show versions?
问题描述
我正在关注 Electron 快速入门指南,它没有任何错误,但是输出与文档中描述的不一样,带有 document.write
的版本不会显示在输出中.
I am following the Electron quick start guide, it works without any errors, but the output is not as described in the docs, the versions with document.write
wont show up in the output.
这是我的输出:
Hello World!
We are using node , Chrome , and Electron .
我的预期输出将包括相应的版本号.
My expected output would include the corresponding version numbers.
我检查了应用程序的 GitHub 页面,还是一样,尝试了各种 StackOverflow 答案,但没有一个对我有用.
I have checked the GitHub page of the app, still the same, tried various StackOverflow answers but none worked for me.
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
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>.
</body>
</html>
package.json
{
"name": "electronapp",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"author": "harsh",
"license": "ISC",
"devDependencies": {
"electron": "^5.0.2"
}
}
main.js
const {app, BrowserWindow} = require('electron')
const path = require('path')
let mainWindow
function createWindow () {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.loadFile('index.html')
mainWindow.on('closed', function () {
mainWindow = null
})
}
app.on('ready', createWindow)
app.on('window-all-closed', function () {
if (process.platform !== 'darwin')
app.quit()
})
app.on('activate', function () {
if (mainWindow === null)
createWindow()
})
我全局安装了 Node,Chrome 是用 Electron 打包的,对吧?
I have Node globally installed, and Chrome is packaged with Electron, right?
推荐答案
如果您激活开发者工具,您应该会在控制台中看到如下错误消息:
If you activate the Developer Tools, you should see error messages in the Console like such:
Uncaught ReferenceError: process is not defined
at index.html:11
您需要激活 nodeIntegration
并停用 contextIsolationrel="nofollow noreferrer">BrowserWindow,这样BrowserWindow中运行的进程(renderer进程")就可以访问Node的process
对象了.
You need to activate nodeIntegration
and deactivate contextIsolation
of the BrowserWindow, so that the process running in the BrowserWindow ("renderer process") is allowed to access Node's process
object.
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
})
(在Electron 12之前,只需要nodeIntegration
键,contextIsolation
的默认值为false
.)
(Before Electron 12, only the nodeIntegration
key was needed, and the default value of contextIsolation
was false
.)
这篇关于运行“First Electron App"不会显示版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!