Electron.remote is undefined

后端 未结 3 449
暖寄归人
暖寄归人 2020-12-16 09:23

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 electr         


        
相关标签:
3条回答
  • 2020-12-16 10:17

    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,
         },
      });
    
    0 讨论(0)
  • 2020-12-16 10:19

    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

    0 讨论(0)
  • 2020-12-16 10:22

    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');
    
    0 讨论(0)
提交回复
热议问题