简体   繁体   中英

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

I am creating an electron-forge app with react template

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

But somehow I only get a white screen, and the developer console shows only this error

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

错误代码

The code of the index.html looks like this:

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

Because the error tells something like require is not defined, I think there is something wrong with node

The versions: Electron-forge: 5.2.4

Electron: v9.1.0

node: v14.5.0

npm: 6.14.7

Operating system: Manjaro Linux

nodeIntegration is now false by default in 5.0.0.

The electronjs FAQ has some sample code on how to set this value.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM