1

I have spent multiple days reading through tickets and documentation trying to find a solution to this issue and no suggestion has worked. It seems a major oversight or I'm a complete dummy.

Simply, what I want to do is access the electron api from within the application, for example I would like a button that one could click to close, resize etc. the window via the BrowserWindow object from within the render/preload scripts.

I am compiling it via electron forge w/ webpack from example repo provided by electron forge (as opposed to separate webpack), I have nodeIntegration: true, contextIsolation: false and enableRemoteModule: true, the package.json also has these options set.

This is the starting project I've used: https://www.electronforge.io/config/plugins/webpack

From reading other tickets the generally recommended way is to require the remote module via various methods, get the window object and close / execute your action. The issue being this always returns null when using:

  • const remote = require('@electron/remote')
  • const remote = require('electron').remote
  • const { remote } = require('electron')

I've tried various sources but for example nothing in this ticket works: Atom Electron - Close the window with javascript

I also read this article that seems to say the remote module was deprecated and is no longer used, however the code in that doesn't work either: https://www.npmjs.com/package/@electron/remote

I have created a git repo with the code I'm trying to use: https://github.com/MajorFailz/electron-problem-example

Please help! I can't really continue development on my project using electron if I can't control the application from within the application. I'm perfectly happy to accept if I've been dumb somehow and missed something vital or fundamental, but as I say it feels like these are the first things any electron noobie would need to know but I'm lost =/

Bonus points if you can provide me a demonstrable example of it working I could look at.

Thanks in advance!

Edit: versions from package

    "@electron-forge/cli": "^6.0.0-beta.61",
    "@electron-forge/maker-deb": "^6.0.0-beta.61",
    "@electron-forge/maker-rpm": "^6.0.0-beta.61",
    "@electron-forge/maker-squirrel": "^6.0.0-beta.61",
    "@electron-forge/maker-zip": "^6.0.0-beta.61",
    "@electron-forge/plugin-webpack": "^6.0.0-beta.61",
    "@vercel/webpack-asset-relocator-loader": "^1.7.0",
    "css-loader": "^6.5.1",
    "electron": "16.0.4",
    "node-loader": "^2.0.0",
    "style-loader": "^3.3.1"
  },
  "dependencies": {
    "electron-squirrel-startup": "^1.0.0"
  }```
MajorFailz
  • 83
  • 1
  • 7
  • What version of electron are you using? – evolutionxbox Dec 16 '21 at 16:03
  • "electron": "16.0.4", have updated ticket – MajorFailz Dec 16 '21 at 16:06
  • 1
    Have you initialised `@electron/remote/main` in the main process? https://www.npmjs.com/package/@electron/remote#migrating-from-remote – evolutionxbox Dec 16 '21 at 16:07
  • Ah, no I hadn't. However I'm getting a new error in the app. ```index.js:495 Uncaught Error: @electron/remote is disabled for this WebContents. Call require("@electron/remote/main").enable(webContents) to enable it. at IpcMainImpl. (C:\Users\Gothi\Documents\workspace\sinbook-client\.webpack\main\index.js:621:61) at IpcMainImpl.emit (node:events:394:28) at Object. (node:electron/js2c/browser_init:161:10774) at Object.emit (node:events:394:28)``` – MajorFailz Dec 16 '21 at 16:15
  • I'm not sure where to get the 'webContents' var – MajorFailz Dec 16 '21 at 16:16
  • 1
    I think it comes from [here](https://www.electronjs.org/docs/latest/api/web-contents/) – evolutionxbox Dec 16 '21 at 16:23
  • 1
    Ah I've gottit, you have to pass the window so `require("@electron/remote/main").enable(mainWindow.webContents)` in the main process. Ah it's finally working! Thanks @evolutionxbox you're an absolute star. – MajorFailz Dec 16 '21 at 16:23

1 Answers1

3

Thanks to @evolutionxbox 's comments I managed to fix this with the following.

npm run install --save @electron/remote

Then in main.js add the following line: require('@electron/remote/main').initialize()

Then finally when you're creating your window in the main process add this: require("@electron/remote/main").enable(mainWindow.webContents)

Now I'm getting the BrowserWindow object when I'm asking for it! Woop!

MajorFailz
  • 83
  • 1
  • 7