const osenv = require('osenv');
Uncaught ReferenceError: require is not defined
在渲染進程中使用osenv獲取User目錄的時候使用require導入osenv包的時候出錯。
修改爲 import { osenv } from 'osenv';依然會報錯。
兩種解決方案:
1、直接在html的script腳本中使用require導包,並使用。
2、在main.js,也就是主進程中寫上集成node的那個工具爲true。
app.on('ready', () => {
mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true, // 是否集成 Nodejs,把之前預加載的js去了,發現也可以運行
}
});
mainWindow.loadURL(`file://${app.getAppPath()}/index.html`);
mainWindow.on('closed', () => { mainWindow = null; });
});