How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process

19,189

Solution 1

I found that all I needed to do was to use the remote module. Electron doesn't allow to directly create a browser window from the renderer process, because it (BrowserWindow) requires ipc module to communicate with the main process. Electron documentation says:

In Electron, GUI-related modules (such as dialog, menu etc.) are only available in the main process, not in the renderer process. In order to use them from the renderer process, the ipc module is necessary to send inter-process messages to the main process.

So, new electron.BrowserWindow() doesn't work. However, using remote module correctly sets up inter-process communicating with the main process and the following modified code works for me:

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

const childWindow = new BrowserWindow({
   width: 800,
   height: 600
});

A more complete explanation of remote module is here: https://electron.atom.io/docs/api/remote/

Solution 2

For anyone that also has this problem and their code isn't inside an electron renderer, you're probably running the script using node script.js, you need to run it using electron script.js.

Share:
19,189

Related videos on Youtube

Elena Filatova
Author by

Elena Filatova

Updated on September 15, 2022

Comments

  • Elena Filatova
    Elena Filatova over 1 year

    I'm using electron to build an application that includes two windows. I'm trying to open a second window from inside renderer process doing something like:

    const electron = require('electron');
    const BrowserWindow = electron.BrowserWindow;
    
    const childWindow = new BrowserWindow({
       width: 800,
       height: 600
    });
    

    I'm getting an error saying

    BrowserWindow is not a constructor.

    My other option is to use window.open, but that is not ideal since that returns BrowserWindowProxy object, which has limited functionality.

  • Salvatore Timpani
    Salvatore Timpani over 3 years
    I am getting TypeError: Cannot read property 'BrowserWindow' of undefined using your above code