gpt4 book ai didi

javascript - Uncaught ReferenceError : require is not defined (electron)

转载 作者:行者123 更新时间:2023-11-30 19:32:17 24 4
gpt4 key购买 nike

我的 Electron 应用程序有问题。大约 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>

index.js

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

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

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

这是我的 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()
})

当我单击最小化或最大化时,没有任何反应。所以我去了http://localhost:8000/html/index.html并检查了控制台,我看到了这些错误

Uncaught ReferenceError: require is not defined at index.html:137

Uncaught ReferenceError: require is not defined at index.js:110

顺便说一句,我使用的是 electron 版本 5.0.2。

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

谢谢。

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

Uncaught ReferenceError: Cannot access 'BrowserWindow' before initialization at window_minimise (index.js:113) at HTMLButtonElement.onclick (index.html:18)

EDIT2:我认为问题是 eel(允许我将 python 与我的 Electron 应用程序连接的代码)要求网页在本地主机上运行,​​因此 require 等节点功能不行。我在这里的 GitHub 问题中进行了更详细的介绍:github.com/ChrisKnott/Eel/issues/147

最佳答案

您需要做的就是在 webPreferences 对象中的 nodeIntegration: true 之后添加 contextIsolation: false

关于javascript - Uncaught ReferenceError : require is not defined (electron),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56303899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com