electron 渲染进程中使用node模块
配置webPreferences
const { app, BrowserWindow } = require('electron');
let mainWindow = null;
app.on('ready', () => {
mainWindow = new BrowserWindow({
width: 800,
height: 800,
// 在渲染进程中使用node, 需要要配置webPreferences属性
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
mainWindow.loadFile('index.html'); // 加载网页
mainWindow.on('close', () => {
mainWindow = null;
})
})
在渲染进程使用node模块
// 引入fs模块
const fs = window.require('fs');
console.dir(fs)
直接使用require的会在node_modules中去查找,而不是node的运行环境,因为要使用
window.require
引用