Required 未定义,使用电子时,我该如何解决? (已经尝试过节点集成)
Required is not defined, when using electron, how do I fix it? (already tried nodeintegration)
所以我正在使用电子来构建一个应用程序。
我需要使用 "fs",但由于我不能要求它,因为要求在 electron 中不起作用,所以我被卡住了。
我已经尝试设置 "nodeItegration: true" 但它仍然不起作用,我也尝试过使用预加载脚本但它仍然不起作用。也许我的预加载脚本有问题?
代码如下。
main.js
const electron = require("electron");
const url = require("url");
const path = require("path");
const {app, BrowserWindow} = electron;
let MainWindow;
app.on("ready", function(){
MainWindow = new BrowserWindow({
webPreferences: {
nodeIntergration: true,
preload: path.join(app.getAppPath(), 'preload.js')
}
});
MainWindow.loadURL(url.format({
pathname: path.join(__dirname, "index.html"),
protocol:"file:",
slashes: true
}));
});
console.log(require.resolve('electron'))
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CommendBot UI</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<button class="safeData-button" onclick="safeData()">Safe</button>
</div>
</div>
<script>
// You can also require other files to run in this process
</script>
<script src="main.js"></script>
<script src="preload.js"></script>
</body>
</html>
preload.js
function safeData() {
var fs = require("fs");
var sampleObject = {
.......(some JSON data)
}
fs.writeFile("./object.json", JSON.stringify(sampleObject, null, 4), (err) => {
if (err) {
console.error(err);
return;
};
console.log("File has been created");
});
}
所以基本上不起作用的是我从 "Safe" 按钮调用的函数,该按钮将函数调用到 fs.writeFile 并写入一个 JSON 文件。
很抱歉把整个代码贴出来,不确定是否更有帮助。
提前感谢您的回答!
问题很可能是一个简单的错字:
MainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true, // Note spelling!
preload: path.join(app.getAppPath(), 'preload.js')
}
});
所以我正在使用电子来构建一个应用程序。 我需要使用 "fs",但由于我不能要求它,因为要求在 electron 中不起作用,所以我被卡住了。
我已经尝试设置 "nodeItegration: true" 但它仍然不起作用,我也尝试过使用预加载脚本但它仍然不起作用。也许我的预加载脚本有问题?
代码如下。
main.js
const electron = require("electron");
const url = require("url");
const path = require("path");
const {app, BrowserWindow} = electron;
let MainWindow;
app.on("ready", function(){
MainWindow = new BrowserWindow({
webPreferences: {
nodeIntergration: true,
preload: path.join(app.getAppPath(), 'preload.js')
}
});
MainWindow.loadURL(url.format({
pathname: path.join(__dirname, "index.html"),
protocol:"file:",
slashes: true
}));
});
console.log(require.resolve('electron'))
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CommendBot UI</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<button class="safeData-button" onclick="safeData()">Safe</button>
</div>
</div>
<script>
// You can also require other files to run in this process
</script>
<script src="main.js"></script>
<script src="preload.js"></script>
</body>
</html>
preload.js
function safeData() {
var fs = require("fs");
var sampleObject = {
.......(some JSON data)
}
fs.writeFile("./object.json", JSON.stringify(sampleObject, null, 4), (err) => {
if (err) {
console.error(err);
return;
};
console.log("File has been created");
});
}
所以基本上不起作用的是我从 "Safe" 按钮调用的函数,该按钮将函数调用到 fs.writeFile 并写入一个 JSON 文件。
很抱歉把整个代码贴出来,不确定是否更有帮助。
提前感谢您的回答!
问题很可能是一个简单的错字:
MainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true, // Note spelling!
preload: path.join(app.getAppPath(), 'preload.js')
}
});