Search code examples
javascriptelectronelectron-builderelectron-packager

Electron Remote (Close, Minimize etc) not work (Cannot read property 'getCurrentWindow' of undefined)


I am trying to add close and minimize button on frameless window in Electron JS app. But its not working in anyhow. It's alwasy show "Cannot read property 'getCurrentWindow' of undefined" error. Here is my code:

const {remote} = require('electron');

document.getElementById('minimize').onclick = function() {
    var window = remote.getCurrentWindow();
    window.minimize();
}

document.getElementById('close').onclick = function() {
    var window = remote.getCurrentWindow();
    window.close();
}

I also add webPreferences: {nodeIntegration: true} in main.js.

win = new BrowserWindow({width: 990, height: 660, title: "Okkhor52 Tools", show: false, resizable: false, frame: false, webPreferences: {nodeIntegration: true}});

Please give me a sollution, I try to found this problem sollution in many place but i didn't get exact sollution.


Solution

  • Sollution is very easy. Just add webPreferences: {enableRemoteModule: true} in your BrowserWindow in main.js.

    win = new BrowserWindow({
        width: 990, 
        height: 660,
        title: "Okkhor52 Tools", 
        resizable: false, 
        frame: false, 
        webPreferences: {
            nodeIntegration: true, 
            enableRemoteModule: true
        }
    }); 
    

    Beside enableRemoteModule: true you will need to add nodeIntegration: true in webPreference, otherwise if you call electron from other's javascript (Like this const {remote} = require('electron'); ) it will be not work.