0

To begin, I would just like to thank you for taking the time to read this post. I would also like to mention that I took the time to read and try to understand most of the exchanges that are made on the subject. Example here : I also took the time to read the "Ultimate Electron Guide" created by reZach at [https://www.debugandrelease.com/the-ultimate-electron-guide/]. I understand the theory behind "electron main process access to require, and anytime our renderer process needs to use require, marshal a request to the main process." ... I took the time to analyze several code examples, I took the time to read the tutorial on preload script here https://www.electronjs.org/docs/latest/tutorial/tutorial-preload but despite all that, I can't adjust an extremely simple code.

Here is my problem, very simple though, but I'm too stupid to find the solution ;). I had no problem to run this code with an Electron versions below v12, but since the adjustment of the security of the nodeintegration, I can't do it anymore. So basically,

in my main.js I create a "browserwindow", standard, with nothing spécial

  win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: false,
      contextIsolation: true,
      enableRemoteModule: false, 
    }
  });

in my html file, I simply have this line

<script src="js/client.js"></script> 

in my client.js I have

let net;
net = require('net');
this.soc = new net.Socket();

How to make so that the module "net" is imported ? ... I know it should be simple though, but I tried a lot of things with a preload.js, a renderer.js, but no way i'm able to make this simple code work without the error "Electron require() is not defined". Someone would be kind enough to help me ? Everything works fine with versions below 12, but I want to adjust to new versions of Electron

LesFerch
  • 1,540
  • 2
  • 5
  • 21
  • see [here](https://stackoverflow.com/questions/54544519/electron-require-is-not-defined), see [here](https://stackoverflow.com/questions/66506331/electron-nodeintegration-not-working-also-general-weird-electron-behavior/66509352#66509352). You can't require stuff when contextIsolation is enabled. You'll need a preload script and use contextbridge to expose methods that send messages to the main process where the work will be done. [API to use](https://www.electronjs.org/docs/latest/api/context-bridge#contextbridgeexposeinmainworldapikey-api) – pushkin Dec 08 '22 at 16:32

0 Answers0