Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: window.require is not a function

Im trying to build an electron app and want to use window.require. Unfortunately the compiler says "TypeError: window.require is not a function". Ironically require works only in main.js.

Here the code Im trying to run:

const electron = window.require('electron') const low =  window.require('lowdb') const FileSync = window.require('lowdb/adapters/FileSync') 

I read in another post that somebody have had the same problem and it was fixed by adding this code into the .html file:

    <script type="text/javascript" src="../../../Gehaltseinstellungen_Hinzufügen.js">         window.nodeRequire = require;         delete window.require;         delete window.exports;         delete window.module;     </script> 

Also the author said using "nodeRequire" instead of require would solve the problem but it doesn't...

Another option I read about is that the NodeIntegration is set to false while the rendering process is activated, but I don't know how to activate Node while rendering.

like image 217
Manfreds3 Avatar asked May 11 '19 14:05

Manfreds3


People also ask

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.

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 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.


1 Answers

It is unclear what version of Electron you are using. The syntax you are using is non-standard.

First – if you are using Electron 5.0, nodeIntegration is false by default in BrowserWindows so you need to specify it explicitly when you create your window:

mainWindow = new BrowserWindow({   width: 800,   height: 600,   webPreferences: {     nodeIntegration: true   } }) 

Given the above, the syntax below works fine (i.e. no 'window' reference needed):

const { ipcRenderer, remote } = require('electron'); 
like image 81
spring Avatar answered Sep 19 '22 22:09

spring