Electronjs window.require not a function

旧巷老猫 提交于 2019-12-11 16:56:43

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!