問題描述
我最初使用的是 electron stable (4.x.x),并且能夠在我的瀏覽器和渲染器進(jìn)程中使用 require
.我升級到 electron beta (5.0.0) 因為我需要更新版本的節(jié)點并且在我的渲染器進(jìn)程中遇到了這個錯誤消息,Uncaught ReferenceError: require is not defined
.
I had initially been using electron stable (4.x.x), and was able to use require
in both my browser and renderer processes. I upgraded to electron beta (5.0.0) because I needed a newer version of node and encountered this error message in my renderer process, Uncaught ReferenceError: require is not defined
.
谷歌搜索并查看電子文檔,我發(fā)現(xiàn)評論說錯誤可能是由于在初始化 BrowserWindow
時將 webPreferences.nodeIntegration
設(shè)置為 false 引起的;例如:new BrowserWindow({width, height, webPreferences: {nodeIntegration: false}});
.但我并沒有這樣做,所以我認(rèn)為一定是其他問題,并繼續(xù)尋找解決方案.
Googling and looking through the electron docs, I found comments saying the error could be caused by setting webPreferences.nodeIntegration
to false when initializing the BrowserWindow
; e.g.: new BrowserWindow({width, height, webPreferences: {nodeIntegration: false}});
. But I was not doing this, so I thought something else must be the issue and continued searching for a resolution.
推薦答案
適用于 Electron 12 及以上版本
For Electron version 12 and above
const electron = require("electron");
const { app, BrowserWindow } = electron;
app.on("ready", () => {
const mainWindow = new BrowserWindow({
width: 1000,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
},
});
mainWindow.loadURL(`file://${__dirname}/index.html`);
});
這篇關(guān)于電子 5.0.0 “未捕獲的參考錯誤:未定義要求"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!