问题描述
我在使用最新版本的 Electron 时遇到此错误。我在我的主要 javascript 文件中将 nodeIntegration 设置为 true 。我已经从一个可用的 Electron 应用程序复制并粘贴了这段代码,但它似乎不适用于我的新应用程序。
电子版:^12.0.0
我的主要 JS
//Require Electron
const { browserWindow,app,autoUpdater,globalShortcut,ipcMain } = require('electron');
const main = require('electron-reload');
//Electron reload
const electron_reload = require('electron-reload')(__dirname);
//disable security warnings
process.env['ELECTRON_disABLE_Security_WARNINGS'] = 'true';
//Main window
function createMain() {
const main = new browserWindow({
minWidth: 425,minHeight: 524,width: 1600,height: 900,frame: null,icon: './assets/icon.png',webPreferences: {
nodeIntegration: true,nodeIntegrationInWorker: true,nodeIntegrationInSubFrames: true,enableRemoteModule: true,}
});
main.loadFile('./HTML/index.html');
main.setMenu(null);
main.webContents.toggleDevTools();
main.webContents.on('did-finish-load',function () {
main.show();
//Ctrl+Shift+I - Open Developer Tools
globalShortcut.register('CommandOrControl+Shift+I',() => {
console.log('Developer Tools Opened');
main.webContents.toggleDevTools();
});
//Make full screen
globalShortcut.register('F11',() => {
main.maximize();
})
});
//Tries to quit the application when main window is closed
main.on('closed',function () {
app.quit();
});
}
//Once the Electron application is initialised (when it is ready) the function createMain is called
app.whenReady()
.then(createMain)
.then(console.log('Launching ChecklistsPro'));
//When the application is launched,if it has no windows open then it will call the createMain function
app.on('activate',() => {
if (browserWindow.getAllWindows().length === 0) {
createMain();
}
});
尝试使用 require
const electron = require('electron');
解决方法
我遇到了同样的问题。 它附带从电子 11.x 到 12.x 的更新,请参见此处:https://www.electronjs.org/releases/stable#release-notes-for-v1200
您必须禁用上下文隔离,它在新的电子版本中从默认情况下变为 true。
function createWindow () {
const win = new BrowserWindow({
width: 800,height: 600,webPreferences: {
nodeIntegration: true,contextIsolation: false
}
})
win.loadFile('index.html')
}
另一个解决方案是降级到电子 11.x,其中 contextIsolation: false 是默认值。
我的消息来源: https://www.reddit.com/r/electronjs/comments/lxjva0/required_not_defined_but_nodeintegration_set_to/ Picture of electron Changelog