Javascript 未捕获引用错误:未定义要求(电子)

Javascript 未捕获引用错误:未定义要求(电子),javascript,electron,Javascript,Electron,我的电子应用程序有问题。大约9个月前我就让它工作了,但现在我定制的最小化和最大化按钮不能正常工作 这是我的文件结构 node_modules ... web css main.css html index.html images ... js index.js themes ... main.js package.json package-lock.json requi

我的电子应用程序有问题。大约9个月前我就让它工作了,但现在我定制的最小化和最大化按钮不能正常工作

这是我的文件结构

node_modules
    ...
web
    css
        main.css
    html
        index.html
    images
        ...
    js
        index.js
    themes
        ...
main.js
package.json
package-lock.json
require.js
下面是
index.html

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="../css/main.css">
        <style type="text/css">* {visibility: hidden;}</style>
        <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600,700" rel="stylesheet">
        <link rel="icon" href="../images/favicon-32x32.png" />
    </head>
    <body>
        <div id="wrapper">
            <div id="title-bar">
                <div id="title-bar-btns">
                    <button id="min-btn" onclick="window_minimise()">&#x2014;</button>
                    <button id="close-btn" onclick="window_close()">&#x2715;</button>
                </div>
            </div>
        ...
        </div>
        <script>
            // You can also require other files to run in this process
            require('../../renderer.js')
        </script>
        <script src="../js/index.js" type="text/javascript"></script>
    </body>
</html>
这是我的
main.js
文件

const {app, BrowserWindow} = require('electron')

let mainWindow

function createWindow () {

  let mainWindow = new BrowserWindow({
      width: 1200,
      height: 748,
      icon:'web/images/favicon-32x32.png',
      resizable: false,
      frame: false,
      show: false,
      backgroundColor: '#171717',
      webPreferences: {
          nodeIntegration: true
      }
  })

  mainWindow.once('ready-to-show', () => {
  mainWindow.show()
})
  mainWindow.setMenu(null);

  mainWindow.loadURL('http://localhost:8000/html/index.html')

  mainWindow.on('closed', function () {
    mainWindow = null
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})

app.on('activate', function () {
  if (mainWindow === null) createWindow()
})

当我单击最小化或最大化时,什么也没有发生。所以我去检查控制台,我看到了这些错误

未捕获引用错误:未在索引中定义require.html:137

未捕获引用错误:未在index.js:110处定义require

顺便说一下,我使用的是电子版5.0.2

如果有人能帮我解决这个问题,那就太好了

谢谢

编辑:我上面提到的错误在页面加载时显示,这个错误在我单击最小化时显示

未捕获引用错误:无法访问之前的“BrowserWindow” 在窗口(index.js:113)处初始化 HTMLButtonElement.onclick(index.html:18)


EDIT2:我认为问题在于eel(允许我将python与我的electron应用程序接口的代码)要求网页在localhost上运行,因此,
require
等节点功能不起作用。我将在GitHub的一期文章中详细介绍一下:GitHub.com/ChrisKnott/Eel/issues/147

如下更改您的
index.js
文件。然后使用
nodeRequire
而不是
require
关键字

  initNodeRequire();
  const {BrowserWindow} = nodeRequire('electron').remote;

  function window_minimise(){
      let window = BrowserWindow.getCurrentWindow();
      window.minimize();
  }

  function window_close(){
      let window = BrowserWindow.getCurrentWindow();
      window.close();
  }

  function initNodeRequire(){
       window.nodeRequire = require;
       delete window.require;
       delete window.exports;
       delete window.module;
  }

看起来你可以找到Answare@TimofeyGoncharov如果你读了我的帖子,你会意识到我已经将nodeIntegration设置为True请提供index.js的完整版本,因为错误指的是上面的特定行file@GJCode我已经提供了周围的线条,包括错误所在,您不能在普通浏览器中使用require。JS在普通浏览器中的沙箱中运行,require使用浏览器无法访问的文件系统。electron所做的是在chromium浏览器中包装您的应用程序,并在沙箱之外运行js,这就是为什么对于electron(和节点),您可以使用require和文件系统。这是您在控制台上得到错误的方式,它们与您的问题无关感谢您的回答,不幸的是,我仍然得到这些错误:
uncaughtreferenceerror:require未在index.html:137中定义,
uncaughtreferenceerror:require未在index.js:109的initNodeRequire(index.js:123)中定义,当我点击minimize
uncaughtreferenceerror:BrowserWindow没有在window\u minimize(index.js:113)中定义在HTMLButtonElement.onclick(index.html:18)
。虽然我不确定这一点,因为我认为
require
是一个不会在浏览器中运行的节点,即使我是通过devtools for electron查看控制台,这实际上只是localhost实例。即使如此,按钮仍然不起作用。我认为问题在于eel(允许我将python与我的electron应用程序连接的代码)要求网页在本地主机上运行,因此像
require
这样的节点功能不起作用。我将在GitHub的一期文章中详细介绍:
  initNodeRequire();
  const {BrowserWindow} = nodeRequire('electron').remote;

  function window_minimise(){
      let window = BrowserWindow.getCurrentWindow();
      window.minimize();
  }

  function window_close(){
      let window = BrowserWindow.getCurrentWindow();
      window.close();
  }

  function initNodeRequire(){
       window.nodeRequire = require;
       delete window.require;
       delete window.exports;
       delete window.module;
  }