問題描述
無法在電子中使用任何電子或節(jié)點相關(guān)操作.獲取錯誤過程未定義.我檢查了他們指導(dǎo)添加節(jié)點支持的各個地方,但這已經(jīng)完成所以卡在這里我的主要應(yīng)用程序代碼是
Unable to use any electron or node related operations in electron . Getting error process not defined. I Checked at various places they guide to add node Support but that is already Done so stucked here My Main Application code is
const electron = require("electron");
const { app, BrowserWindow } = 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();
}
});
還有Index.html
And Index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello World!</title>
</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>
推薦答案
更新:下面的答案是一種解決方法.您不應(yīng)禁用 contextIsolation
,也不應(yīng)啟用 nodeIntegration
.相反,您應(yīng)該使用 預(yù)加載腳本 和 contextBridge API.
Update: the answer below is a workaround. You should not disable contextIsolation
and you should not enable nodeIntegration
. Instead you should use a preload script and the contextBridge API.
在 Electron 12 中,contextIsolation
現(xiàn)在默認為 true
In Electron 12, contextIsolation
is now by default true
如果您將其設(shè)置為 false
,您將可以在渲染器進程中訪問 Node.js API
If you set it to false
, you will have access to Node.js APIs in the renderer process
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
contextIsolation: false,
nodeIntegration: true
},
});
win.loadFile("index.html");
}
??需要注意的是,不推薦這樣做!
Electron 維護者更改默認值是有充分理由的.請參閱此討論
There's a good reason why Electron maintainers changed the default value. See this discussion
如果沒有 contextIsolation,渲染器進程中運行的任何代碼都可以很容易地進入 Electron 內(nèi)部或您的預(yù)加載腳本,并執(zhí)行您不希望任意網(wǎng)站執(zhí)行的特權(quán)操作.
Without contextIsolation any code running in a renderer process can quite easily reach into Electron internals or your preload script and perform privileged actions that you don't want arbitrary websites to be doing.
這篇關(guān)于無法在渲染器進程中使用 Node.js API的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!