71

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.

modernator
  • 4,341
  • 12
  • 47
  • 76
  • 1
    Are you running this code in a render process? – Max Jun 17 '16 at 17:55
  • No, it is main process. – modernator Jun 18 '16 at 03:30
  • 1
    Well there is your problem then. `remote` is needed only to require other modules from inside a render process. In the main process just get your modules directly from `require('electron')`. Which it looks like you do, you just need to remove `remote`... – Max Jun 18 '16 at 03:34
  • @Teak You should convert your comment into an answer. – Vadim Macagon Jun 18 '16 at 03:51
  • Thanks, I think I misunderstood of Electron remote module, now I solved my problem. Thank you for your answer Teak. Like Vadim said, it will be good write your comment to answer. – modernator Jun 18 '16 at 03:53
  • For Electron 14 and later: https://stackoverflow.com/a/69297584/1768303 – noseratio Sep 23 '21 at 22:27

5 Answers5

162

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');
Max
  • 4,529
  • 1
  • 25
  • 29
  • 4
    `import { remote } from 'electron';` in typescript and in renderer process throws error on windows but works fine on mac os. – Alexey Sh. Apr 10 '19 at 16:01
  • 7
    I just get `Uncaught TypeError: Cannot destructure property 'BrowserWindow' of 'remote' as it is undefined.` – Barkermn01 Sep 07 '20 at 00:38
  • 7
    Thank you so much for your thoughtful update of your answer in 2020! I couldn't understand why my "remote" was undefined. – Jason Fetterly Oct 03 '20 at 13:53
  • 1
    `enableRemoteModule: true` is what being required on newer version of Electron. – Aryo Oct 20 '20 at 15:50
  • 13
    I have enableRemoteModule set to true, but I am still facing the same error. – David Essien Jan 02 '21 at 02:35
  • 1
    Electron 14 doesn't have ``enableRemoteModule: true`` property anymore, yet without it ``@electron/remote`` doesn't work as it requires that property to be set :) – nazgul Aug 31 '21 at 17:53
  • 6
    Using v17 of electron. the remote prop is not available in the electron import – Robert Cabri Feb 04 '22 at 14:10
65

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

frogatto
  • 28,539
  • 11
  • 83
  • 129
Yumick Gharti
  • 1,138
  • 1
  • 7
  • 7
26

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()
frogatto
  • 28,539
  • 11
  • 83
  • 129
Ha0ran
  • 585
  • 5
  • 13
6

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,
     },
  });
crispengari
  • 7,901
  • 7
  • 45
  • 53
3

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,
    }
Kanish R
  • 41
  • 1
  • 5
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). – Tyler2P Sep 17 '21 at 19:47
  • You can check answer from @Ha0ran , remote has been removed from electon's package from v14 above. – GoldenArcher Sep 02 '22 at 03:40