在Electron中,渲染進程與主進程交互時,使用require報錯

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; });
});

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章