简体   繁体   English

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

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

I am trying to get set up on a simple Electron project, but am having trouble resolving this issue. 我试图建立一个简单的Electron项目,但无法解决此问题。 I have a file add.js that is used to add an event listener to a button to close a frameless browser window. 我有一个文件add.js ,用于将事件监听器添加到按钮以关闭无框架浏览器窗口。 I have the add.js file imported in a script tag to the HTML like so: 我将脚本标记中的add.js文件导入到HTML中,如下所示:

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

The add.js file only contains this event listener: 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();
})

If it is needed, here is index.js , the click event listener to open this window: 如果需要,这里是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)
})

The problem that I am having is that the frameless browser window opens just fine, but I get the "Uncaught ReferenceError:" immediately and the JS code does not load. 我遇到的问题是无框浏览器窗口可以很好地打开,但是我立即收到“ Uncaught ReferenceError:”,并且未加载JS代码。

I've tried looking up the issue on here (stackoverflow) but the answers suggest that this is caused by attempting to run a node program in the browser. 我尝试在此处查找问题(stackoverflow),但答案表明这是由于尝试在浏览器中运行节点程序引起的。 However, the index.js code that I included above works just fine, with no errors. 但是,我上面包含的index.js代码运行正常,没有错误。


Here is the code to the HTML files as well, just in case I'm going crazy: 这也是HTML文件的代码,以防万一我发疯:

index.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 : 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>

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Adding 添加

webPreferences: {
            nodeIntegration: true
},

to the new BrowserWindow instance fixed this issue for me. 新的BrowserWindow实例的更新为我解决了此问题。

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

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

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