Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron.remote is undefined

I have trouble with using Electron. As you can see the title, when i load remote module, it saids it is undefined. This is the code of entry js:

const electron = require('electron');
const { app, BrowserWindow, Tray, remote, ipcMain } = electron;

function initApp() { ... }

app.on('ready', () => {
    initApp();

    console.log(electron);         // object, but no remote inside
    console.log(electron.remote);  // undefined
    console.log(remote);           // undefined
});

and i tried to follow official doc here: http://electron.atom.io/docs/api/remote/

with

const { remote } = electron;
const { BrowserWindow } = remote;

let win = new BrowserWindow({width: 800, height: 600});  // error! BrowserWindow is not a constructor blabla

...
remote.getCurrentWindow().focus();

i don't know what am i missing. any advice will very appreciate.

like image 295
modernator Avatar asked Jun 17 '16 14:06

modernator


People also ask

What is electron remote?

@electron/remote is an Electron module that bridges JavaScript objects from the main process to the renderer process. This lets you access main-process-only objects as if they were available in the renderer process.

What is electron Nodeintegration?

Electron node integration refers to the ability of accessing Node. js resources from within the “renderer” thread (the UI). It is enabled by default in Quasar CLI, although Electron is encouraging developers to turn it off as a security precaution.


5 Answers

Update 2020, since this answer still appears at the top. For the original answer to work in current versions of Electron, you need to set enableRemoteModule when creating the window in your main process.

const myWindow = new BrowserWindow({
    webPreferences: {
        enableRemoteModule: true
    }
}); 

Original answer:

remote is needed only to require other modules from inside a render process. In the main process you just get your modules directly from require('electron'). Which it looks like is done in the example just with remote unnecessarily added.

Render process:

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

Main process:

const { BrowserWindow } = require('electron');
like image 193
Teak Avatar answered Oct 04 '22 12:10

Teak


In electron 10.0.0, remoteModule is set false by default. So, if you want to use const {BrowserWindow, dialog } = require('electron').remote; in JavaScript file, then you must set enableRemoteModule as true in webPreferences.

const w = new BrowserWindow({
    webPreferences: {
        enableRemoteModule: true
    }
}); 

link: https://github.com/electron/electron/blob/master/docs/breaking-changes.md#default-changed-enableremotemodule-defaults-to-false

like image 45
Yumick Gharti Avatar answered Oct 04 '22 12:10

Yumick Gharti


The remote module was deprecated in Electron 12, and will be removed in Electron 14. It is replaced by the @electron/remote module.

// Deprecated in Electron 12:
const { BrowserWindow } = require('electron').remote
// Replace with:
const { BrowserWindow } = require('@electron/remote')

// In the main process:
require('@electron/remote/main').initialize()
like image 45
Ha0ran Avatar answered Oct 04 '22 13:10

Ha0ran


remote becomes undefined sometimes in electron all you have to do is to go to your main.js and add the following object while creating a window under webPreference set enableRemoteModule: true as shown bellow then your problem will be solved

 win = new BrowserWindow({
    width: 700,
    height: 600,
    hasShadow: true,
    webPreferences: {
      nodeIntegration: true,
      enableRemoteModule: true,
     },
  });
like image 25
crispengari Avatar answered Oct 04 '22 11:10

crispengari


i enabled remote module, still getting

index.html:43 Uncaught TypeError: Cannot read properties of undefined (reading 'getCurrentWindow')

for

const remote = require('electron').remote;

(or)

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

while using

remote.getCurrentWindow().close();

i did add

webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
            enableRemoteModule: true,
    }
like image 25
Kanish R Avatar answered Oct 04 '22 13:10

Kanish R