Search code examples
electronrequire

Electron Uncaught ReferenceError: require is not defined | I have nodeIntegration set to true


I am getting this error whilst using the latest version of Electron. I have nodeIntegration set to true in my main javascript file. I have copy and pasted this code from a working Electron application but it doesn't seem to work with my new app.

Electron Version: ^12.0.0

My main 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();
    }
});

Trying to use require

const electron = require('electron');

Solution

  • I have had the same issue. It comes with the update from electron 11.x to 12.x See here: https://www.electronjs.org/releases/stable#release-notes-for-v1200

    You have to disable contextisolation, which changed from beeing true by default in the new electron version.

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

    Another solution is to downgrade to electron 11.x where contextIsolation: false is the default.

    My sources: https://www.reddit.com/r/electronjs/comments/lxjva0/required_not_defined_but_nodeintegration_set_to/ Picture of electron Changelog