我正在尝试fs在我的renderer流程中使用 Node 模块(在本例中为),如下所示:
// main_window.js
const fs = require('fs')
function action() {
console.log(fs)
}
Run Code Online (Sandbox Code Playgroud)
注意:action当我按下main_window.
但这给出了一个错误:
Uncaught ReferenceError: require is not defined
at main_window.js:1
Run Code Online (Sandbox Code Playgroud)
我可以解决这个问题,正如这个接受的答案所建议的那样,通过main.js在初始化时将这些行添加到我的main_window:
// main.js
main_window = new BrowserWindow({
width: 650,
height: 550,
webPreferences: {
nodeIntegration: true
}
})
Run Code Online (Sandbox Code Playgroud)
但是,根据 docs,这不是最好的做法,我应该创建一个preload.js文件并在那里加载这些 Node 模块,然后在我的所有renderer进程中使用它。像这样:
main.js:
main_window = new BrowserWindow({
width: 650,
height: 550,
webPreferences: {
preload: path.join(app.getAppPath(), 'preload.js') …Run Code Online (Sandbox Code Playgroud)