M
M
MarkSmersh2021-09-30 23:24:14
JavaScript
MarkSmersh, 2021-09-30 23:24:14

How to fix "require is not defined" in Electron?

When trying to request in renderer.js: writes that "require is not defined" In the parameters of the window, webPrefereces is nodeIntegration: true, contextIsolation: false
const { ipcRenderer } = require('electron')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Zhuravlev, 2021-10-22
@zhuravlevkk

Create (modify) preload.js.
There you need to add

const {contextBridge, ipcRenderer} = require("electron");
contextBridge.exposeInMainWorld('ipcRenderer', ipcRenderer)

In main.js in the function where you create the BrowserWindow object
mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            preload: path.join(app.getAppPath(), 'preload.js')
          }
    });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question