3

I've been experiencing some issues with an Electron + Create React App app I'm making. It's an offline app for cost calculation, I need to persist a couple user settings, for this I used https://github.com/sindresorhus/electron-store. Like with most electron's modules, I have to import it as:

const Store = window.require("electron-store");

To avoid webpack's conflicts. By searching I found that for most people setting nodeIntegration: true when creating electron's BrowserWindow would avoid the problem, but it's not my case, I keep getting the same error.

What I've already tried:

  1. Using plain require: It results in TypeError: fs.existsSync is not a function, and in console: Can't resolve 'worker_threads' in '...\node_modules\write-file-atomic'

  2. Use a module to override webpack config: I used craco to set target to electron-renderer. It results in a blank page when I launch the app, with an error in devtools telling ReferenceError: require is not defined

Additional info is that I'm not using typescript but plain js so using "declare global" and such won't work

My public/electron.js file:

const electron = require("electron");
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const path = require("path");
const isDev = require("electron-is-dev");
let mainWindow;
function createWindow() {
 mainWindow = new BrowserWindow({
 width: 900,
 height: 680,
 webPreferences: {
 nodeIntegration: true
 }
 });
 mainWindow.loadURL(
 isDev
 ? "http://localhost:3000"
 : `file://${path.join(__dirname, "../build/index.html")}`
 );
 mainWindow.on("closed", () => (mainWindow = null));
 if (!isDev) mainWindow.setMenu(null);
}
app.on("ready", createWindow);
app.on("window-all-closed", () => {
 if (process.platform !== "darwin") {
 app.quit();
 }
});
electron.app.allowRendererProcessReuse = true;
app.on("activate", () => {
 if (mainWindow === null) {
 createWindow();
 }
});
Erick
1,2549 silver badges17 bronze badges
asked Apr 3, 2020 at 23:28
1
  • Can you post how you create your BrowserWindow including all it's configuration. Commented Apr 3, 2020 at 23:40

2 Answers 2

5

You have tu create a preoload script on electron.

  1. Create a file named preload.js on the directory where you have de electron main script, Put this code on it:

    window.require = require;

  2. Go to your electron main script and type this in the code where you create the window:

    win = new BrowserWindow({ width: 1280, height: 720, webPreferences: { nodeIntegration: false, preload: __dirname + '/preload.js' },

    }) With this you will preload the script before all, this fixed the problem for me, I hope also for you.

answered Jul 25, 2020 at 4:29
Sign up to request clarification or add additional context in comments.

3 Comments

this is solved the problem to me after 1000 failed attempt
My electron version is 12.0.0. And preload.js works well but the property that I set in preload.js of window just disappear after window popup. So I can't access the property.
@neosarchizo You probably need to set webPreferences.contextIsolation = false. An alternative that seems like it should work (in newer electron versions) is require("electron").contextBridge.exposeInMainWorld("require", require), but that didn't actually work when I tried it. (it said Cannot find module 'XXX' in renderer; not a good idea anyway to expose the require function in renderer, but wanted to try it)
3

Electron 12 changed the default setting of contextIsolation for webPreferences, as documented in their breaking changes for Electron 12. Setting it to false will allow you access to require() again, as per their notes:

In Electron 12, contextIsolation will be enabled by default. To restore the previous behavior, contextIsolation: false must be specified in WebPreferences.

We recommend having contextIsolation enabled for the security of your application.

Another implication is that require() cannot be used in the renderer process unless nodeIntegration is true and contextIsolation is false.

mainWindow = new BrowserWindow({
 width: 900,
 height: 680,
 webPreferences: {
 contextIsolation: false,
 nodeIntegration: true
 }
}
answered Oct 14, 2021 at 21:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.