MajorFailz
MajorFailz

Reputation: 83

Electron functions are inaccessible from within renderer/preload scripts even with recommended settings

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:

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"
  }```

Upvotes: 1

Views: 802

Answers (1)

MajorFailz
MajorFailz

Reputation: 83

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!

Upvotes: 3

Related Questions