Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

electron 5.0.0 "Uncaught ReferenceError: require is not defined"

I had initially been using electron stable (4.x.x), and was able to use require in both my browser and renderer processes. I upgraded to electron beta (5.0.0) because I needed a newer version of node and encountered this error message in my renderer process, Uncaught ReferenceError: require is not defined.

Googling and looking through the electron docs, I found comments saying the error could be caused by setting webPreferences.nodeIntegration to false when initializing the BrowserWindow; e.g.: new BrowserWindow({width, height, webPreferences: {nodeIntegration: false}});. But I was not doing this, so I thought something else must be the issue and continued searching for a resolution.

like image 498
junvar Avatar asked Mar 11 '19 00:03

junvar


People also ask

How do you fix require is not defined?

To solve the "ReferenceError require is not defined" error, remove the type property if it's set to module in your package. json file and rename any files that have a . mjs extension to have a . js extension.

What is preload JS in electron?

Summary​ A preload script contains code that runs before your web page is loaded into the browser window. It has access to both DOM APIs and Node. js environment, and is often used to expose privileged APIs to the renderer via the contextBridge API.

What is Nodeintegration electron?

Electron node integration refers to the ability of accessing Node. js resources from within the “renderer” thread (the UI). It is enabled by default in Quasar CLI, although Electron is encouraging developers to turn it off as a security precaution.

What is electron remote?

@electron/remote is an Electron module that bridges JavaScript objects from the main process to the renderer process. This lets you access main-process-only objects as if they were available in the renderer process.


1 Answers

For Electron version 12 and above

const electron = require("electron");  const { app, BrowserWindow } = electron;  app.on("ready", () => {   const mainWindow = new BrowserWindow({     width: 1000,     height: 600,     webPreferences: {       nodeIntegration: true,       contextIsolation: false,       enableRemoteModule: true,     },   });   mainWindow.loadURL(`file://${__dirname}/index.html`); }); 
like image 90
Vigneshwaran Chandrasekaran Avatar answered Sep 23 '22 13:09

Vigneshwaran Chandrasekaran