Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 371
Nurul Alom Ador Avatar asked Nov 30 '22 13:11

Nurul Alom Ador


2 Answers

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.

like image 59
Nurul Alom Ador Avatar answered Dec 04 '22 07:12

Nurul Alom Ador


If you already have enableRemoteModule set to true and are still having this problem, it could very well be that you are requiring the remote module, or requiring a file that requires it inside the main process, at least this was the case for me. The remote module is not necessary inside the main process and is only for the renderer process.

If you require or require another javascript file that uses require("electron").remote.getCurrentWindow() inside the main process then you will get this error.

Solve this simply by not including that code in any way inside the main process.

like image 45
avisk Avatar answered Dec 04 '22 08:12

avisk