gpt4 book ai didi

electron - nodeIntegration 真的被忽略了吗?

转载 作者:行者123 更新时间:2023-12-03 12:25:51 29 4
gpt4 key购买 nike

我正在尝试在附加到 html 文件的 js 文件中使用 require 指令。但是,我收到该文件的要求未定义的错误消息。我的理解是在 5.0 之后的较新版本的 Electron 中,默认情况下禁用了 nodeIntegration。但是,即使在 Web 首选项中启用 nodeIntegration 后,我仍然收到需要错误消息。我的理解是这个 nodeIntegration 应该可以解决这个问题。为什么我仍然遇到 require 未定义的问题?这是 main.js 文件的相关部分。

编辑:我在 preload 和 nodeIntegration 之间缺少一个逗号,所以感谢指出这一点的人!但是,我仍然遇到这个问题。仍然收到“未捕获的 ReferenceError:需要未定义”。

第二次编辑:这是该问题的最小再现。即使将 nodeIntegration 设置为覆盖它, require 函数也未定义。最后,我只是尝试从本地 json 文件中读取数据,但是在使用 Electron 时,我能找到的每个实例都以一种简单的方式进行读取,无论是需要 fs 还是需要文件,都以某种方式使用了 require。如果 require 语句根本不起作用,我将无法进行任何工作。

主要.js:

// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow

function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration:true
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')

// Open the DevTools.
// mainWindow.webContents.openDevTools()

// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') app.quit()
})

app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) createWindow()
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.


索引.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet">
<link href="main.css" rel="stylesheet">
<title>Destiny Guide</title>
<script src="problem.js"></script>
</head>
<body>
<h1>Hello World</h1>
Hello World
<br>
<h1><a href = "HelloWorld">helloWorld</a><h1>

<!-- You can also require other files to run in this process -->
<script src="./renderer.js"></script>
</body>
</html>

问题.js:

var dataArc = require("./ZeroHourArc.json");

第三,也是最后,编辑:见下面的答案。

最佳答案

只需尝试添加 contextIsolation: false像这样:

mainWindow = new BrowserWindow({
width: 500,
height: 500,
icon: './public/logo.png',
backgroundColor: 'white',
webPreferences: {nodeIntegration: true, contextIsolation: false}
});

关于electron - nodeIntegration 真的被忽略了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59523682/

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