【问题标题】:Electron Uncaught ReferenceError: require is not defined | I have nodeIntegration set to trueElectron Uncaught ReferenceError: require is not defined |我将 nodeIntegration 设置为 true
【发布时间】:2021-06-05 18:21:40
【问题描述】:

我在使用最新版本的 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');

【问题讨论】:

    标签: electron require


    【解决方案1】:

    我也遇到过同样的问题。 它附带从电子 11.x 到 12.x 的更新,请参见此处:https://www.electronjs.org/releases/stable#release-notes-for-v1200

    您必须禁用 contextisolation,它在新的电子版本中默认情况下会更改为 true。

    function createWindow () {
      const win = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
          nodeIntegration: true,
          contextIsolation: false
        }
      })
    
      win.loadFile('index.html')
    }
    

    另一种解决方案是降级到 electron 11.x,其中 contextIsolation: false 是默认值。

    我的消息来源: https://www.reddit.com/r/electronjs/comments/lxjva0/required_not_defined_but_nodeintegration_set_to/ Picture of electron Changelog

    【讨论】:

    • 谢谢!这正是互联网所缺少的
    猜你喜欢
    • 2019-09-29
    • 2013-08-14
    • 2017-02-25
    • 1970-01-01
    • 2021-11-08
    • 2021-01-15
    • 2017-08-15
    • 1970-01-01
    • 2020-12-26
    相关资源
    最近更新 更多