问题
I'm using create-react-app (react-scripts v3.0.0) and electronjs (v5.0.1). I'm trying to pass events from the renderer to main process using the 'icpMain' module as described here, but get the error window.require
is not a function for the line
const { ipcRenderer } = window.require('electron');
How can I get require
into the global scope in the renderer process? Or is there another way of communicating between the main and renderer process?
Edit:
I've tried removing the react build entirely and get the same results simply using the electron example code in index.html.
回答1:
It looks like adding the preference:
var mainWindow = new electron.BrowserWindow({
...
webPreferences: {
nodeIntegration: true,
}
});
is needed to enable require
in the renderer process.
回答2:
Indeed, you have to set nodeIntegration
to true
in your BrowserWindow webPreferences since the version 5.0.0 the default values of nodeIntegration and webviewTag are false to improve security. Electron associated PR: 16235
来源:https://stackoverflow.com/questions/56265958/electronjs-window-require-not-a-function