Javascript Electron应用程序未预加载js文件(从客户端读取本地fs)
我正在尝试在一个Electron应用程序中安装一个前端React组件(大约两周前由CRA制造),读取我的本地(Windows)文件系统,以便我可以搜索它(例如,在我的文档文件夹中搜索名为“waffles”的文件)。为了做到这一点,我尝试在客户端上使用Javascript Electron应用程序未预加载js文件(从客户端读取本地fs),javascript,reactjs,electron,Javascript,Reactjs,Electron,我正在尝试在一个Electron应用程序中安装一个前端React组件(大约两周前由CRA制造),读取我的本地(Windows)文件系统,以便我可以搜索它(例如,在我的文档文件夹中搜索名为“waffles”的文件)。为了做到这一点,我尝试在客户端上使用fs,如下所示: fsMod.js: const remote = window.require("electron").remote; const electronFs = remote.require("fs&quo
fs
,如下所示:
fsMod.js:
const remote = window.require("electron").remote;
const electronFs = remote.require("fs");
export default async function (string) {
if (string.length) {
electronFs.readdir("./", (err, files) => {
files.forEach((file) => {
console.log(file);
});
return "found files!";
});
}
return null;
}
electron/main.js:
const { app, BrowserWindow, globalShortcut } = require("electron");
const isDev = require("electron-is-dev");
const path = require("path");
let mainWindow;
function createWindow() {
mainWindow = new BrowserWindow({
width: 400,
height: 300,
// useContentSize: true,
show: true,
frame: false,
transparent: true,
webPreferences: {
nodeIntegration: true,
preload: `${__dirname}/preload.js`,
},
});
const startURL = isDev
? "http://localhost:3000/app"
: `file://${path.join(__dirname, "../build/index.html")}`;
mainWindow.loadURL(startURL);
mainWindow.once("ready-to-show", () => {
// mainWindow.show()
mainWindow.setSize();
});
mainWindow.on("closed", () => {
mainWindow = null;
});
}
app.on("ready", createWindow);
electron/preload.js:
window.preloadWasRun = "---***PRELOADING"; // added to see if this was running at all; it's not visible
window.require = require;
然而,当我的Electron应用程序启动时,它告诉我,无法读取未定义的属性“require”。(奇怪的是,当我在Chrome中打开它时,它告诉我稍微不同的是,window.require不是一个函数。这告诉我没有定义窗口
,并且preload.js
没有运行
如果有更简单的方法来读取本地文件系统,我也愿意这样做;我怀疑从客户端做这件事是不对的,而electron的main.js
文件可能可以替代它;不知道是怎么回事。什么是window.require在electron中没有window.require,为什么要将require分配给window.require?@ash我在这里发现:你检查过这个部分了吗?const remote=window.require('electron').remote@ash不知道你的意思,但我确实在require('electron')
部分之前注销了窗口
,它没有.require
道具,是吗?