"Uncaught ReferenceError: require is not defined" in index.html of electron-forge app with react template

"Uncaught ReferenceError: require is not defined" in index.html of electron-forge app with react template

我正在使用 React 模板创建一个 electron-forge 应用程序

electron-forge init myApp --template=react
cd myApp
npm install

但不知何故我只得到一个白屏,开发者控制台只显示这个错误

"Uncaught ReferenceError: require is not defined" at index.html:inline_0.js:2

index.html 的代码如下所示:

<html lang="en">

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
</head>

<body style="overflow: hidden; background-color: rgba(0,0,0,0); margin: 0">
  <div id="App"></div>
</body>

<script>
  import React from 'react';
  import ReactDOM from 'react-dom';
  import { AppContainer } from 'react-hot-loader';
  const render = () => {
    const App = require('./app').default;
    ReactDOM.render(<AppContainer><App /></AppContainer>,
      document.getElementById('App'));
  }
  render();
  if (module.hot) {
    module.hot.accept(render);
  }
</script>


</html>

因为报错说require is not defined,我觉得node有问题

版本: 电子锻造:5.2.4

电子:v9.1.0

节点:v14.5.0

npm: 6.14.7

操作系统:Manjaro Linux

nodeIntegration 现在在 5.0.0 中默认为 false。

electronjs FAQ 有一些关于如何设置这个值的示例代码。

let win = new BrowserWindow({
  webPreferences: {
    nodeIntegration: true
  }
})
win.show()