Electron実行時に以下のエラーが出た。
Uncaught ReferenceError: exports is not defined at renderer.js:2
レンダラープロセスでnodejsを使おうとして上記のエラーが発生した。
簡単な解決策
window作成時にnodeIntegrationを有効にする。ただし有効にするのはセキュリティ上良くない。
main.ts
const mainWindow = new BrowserWindow({
height: 600,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
nodeIntegration: true // 追加
},
width: 800,
});
index.htmlにexportsを定義する
index.html
<script> var exports = {}; </script>
noteIntegration
レンダラープロセスでnodejsの機能が使えるとxss脆弱性があった時に被害が大きくなるためデフォルトではnodejsの機能が使えなくなっている。
別の回避策はcontextBridgeを使う方法がある。