最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Electron JS - Cannot destructure property 'BrowserWindow' of 'require(...).remote'

programmeradmin0浏览0评论

This is in Renderer Process:


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

const path = require('path')
const url = require('url')

const newWindowButton = document.getElementById('new-window-btn');
newWindowButton.addEventListener('click',(e)=>{
    let win3 = new BrowserWindow();
    win3.loadURL(url.format({
        pathname: path.join(__dirname,'index3.html'),
        protocol: "file",
        slashes: true
    }))

})

I am not able to open a new window in renderer process, getting the below error.

**Uncaught TypeError: Cannot destructure property 'BrowserWindow' of 'require(...).remote' as it is

undefined.**
    at Object.<anonymous> (D:\ElectronTute\helloWorld\index1.js:4)
    at Object.<anonymous> (D:\ElectronTute\helloWorld\index1.js:21)
    at Module._compile (internal/modules/cjs/loader.js:1145)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js`enter code here`:1166)
    at Module.load (internal/modules/cjs/loader.js:981)
    at Module._load (internal/modules/cjs/loader.js:881)
    at Function.Module._load (electron/js2c/asar.js:769)
    at Module.require (internal/modules/cjs/loader.js:1023)
    at require (internal/modules/cjs/helpers.js:77)
    at index1.html:13

This is in Renderer Process:


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

const path = require('path')
const url = require('url')

const newWindowButton = document.getElementById('new-window-btn');
newWindowButton.addEventListener('click',(e)=>{
    let win3 = new BrowserWindow();
    win3.loadURL(url.format({
        pathname: path.join(__dirname,'index3.html'),
        protocol: "file",
        slashes: true
    }))

})

I am not able to open a new window in renderer process, getting the below error.

**Uncaught TypeError: Cannot destructure property 'BrowserWindow' of 'require(...).remote' as it is

undefined.**
    at Object.<anonymous> (D:\ElectronTute\helloWorld\index1.js:4)
    at Object.<anonymous> (D:\ElectronTute\helloWorld\index1.js:21)
    at Module._compile (internal/modules/cjs/loader.js:1145)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js`enter code here`:1166)
    at Module.load (internal/modules/cjs/loader.js:981)
    at Module._load (internal/modules/cjs/loader.js:881)
    at Function.Module._load (electron/js2c/asar.js:769)
    at Module.require (internal/modules/cjs/loader.js:1023)
    at require (internal/modules/cjs/helpers.js:77)
    at index1.html:13
Share Improve this question edited Sep 25, 2020 at 2:10 tpikachu 4,8542 gold badges21 silver badges45 bronze badges asked Sep 15, 2020 at 11:52 DevendraDevendra 551 silver badge5 bronze badges 2
  • Does this answer your question? Electron.remote is undefined – snwflk Commented Sep 15, 2020 at 16:16
  • In particular the new answers (enableRemoteModule) – snwflk Commented Sep 15, 2020 at 16:17
Add a comment  | 

1 Answer 1

Reset to default 19
 mainWindow = new BrowserWindow({
    width: 1280,
    height: 960,
    webPreferences: {
      nodeIntegration: true,
      enableRemoteModule: true,
     },
  });

I believe you are using the new version of Electron. From the v9 version, we are not allowed to use remote on the renderer unless set the enableRemoteModule as true.

Plus, to load node_moduels on the renderer by using require(), we need to enable the nodeIntegration as well. As require is one of the node APIs.

https://github.com/electron/electron/issues/21408

For me, I'm exposing only necessary APIs via the preload script.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论