繁体   English   中英

未捕获的ReferenceError:在add.js:1上未定义require

[英]Uncaught ReferenceError: require is not defined at add.js:1

我试图建立一个简单的Electron项目,但无法解决此问题。 我有一个文件add.js ,用于将事件监听器添加到按钮以关闭无框架浏览器窗口。 我将脚本标记中的add.js文件导入到HTML中,如下所示:

 <script src="add.js"></script>

add.js文件仅包含以下事件侦听器:

const electron = require('electron')
const remote = electron.remote
const closeBtn = document.getElementById('closeBtn')

closeBtn.addEventListener('click', (event) => {
    let window = remote.getCurrentWindow();
    window.close();
})

如果需要,这里是index.js ,单击事件侦听器可打开此窗口:

const electron = require('electron');
const path = require('path');
const BrowserWindow = electron.remote.BrowserWindow;

const notifyBtn = document.getElementById('notifyBtn');

notifyBtn.addEventListener('click', (event) => {
    const modalPath = path.join('file://', __dirname, 'add.html')
    let win = new BrowserWindow({
        alwaysOnTop: true,
        frame: false,
        transparent: true,
        width: 400,
        height: 200
    })
    win.loadURL(modalPath)
    win.show()
    win.webContents.openDevTools();

    win.on('close', () => win = null)
})

我遇到的问题是无框浏览器窗口可以很好地打开,但是我立即收到“ Uncaught ReferenceError:”,并且未加载JS代码。

我尝试在此处查找问题(stackoverflow),但答案表明这是由于尝试在浏览器中运行节点程序引起的。 但是,我上面包含的index.js代码运行正常,没有错误。


这也是HTML文件的代码,以防万一我发疯:

index.html

<!DOCTYPE html>
<html>

  <head>
    <meta charset="UTF-8" />
    <title>BTCAlert</title>
    <link rel="stylesheet" href="../assets/css/main.css">
  </head>

  <body>
    <div class="row">
      <div id="price-container">
        <p class="subtext">Current BTC USD</p>
        <h1 id="price">Loading..</h1>
      </div>
      <div id="goal-container">
        <p><img src="../assets/images/up.svg"><span id="targetPrice">Choose a Target Price</span></p>
      </div>
      <div id="right-container">
        <button id="notifyBtn">Notify Me When...</button>
      </div>
    </div>
    <script src="index.js"></script>
  </body>

</html>

add.html

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <link rel="stylesheet" href="../assets/css/main.css">
        <link rel="stylesheet" href="../assets/css/add.css">
    </head>

    <body>
        <p class="notify">Notify me when BTC reaches..</p>

        <div class="row2">
            <div>
                <input id="notifyVal" placeholder="USD">
            </div>
            <button id="updateBtn">Update</button>
        </div>

        <a id="closeBtn">Close Window</a><br>

        </div>
        <script src="add.js"></script>
    </body>

</html>

任何帮助是极大的赞赏。

添加

webPreferences: {
            nodeIntegration: true
},

新的BrowserWindow实例的更新为我解决了此问题。

require()函数不能在客户端使用。...如果您想在客户端使用它,则应在此处使用require.jshead.js。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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