†でおきしブログ†

ウナギ食べたいヽ(•̀ω•́ )ゝ✧

【Electron v5.0.2】レンダラープロセス側でexportもrequireも is not definedになるぞちくしょうめ!

f:id:deokisikun:20190529235408p:plain

☆解決方法

  • メインプロセス側のBrowserWindow作成時にnodeIntegrationをtrueにする
  • レンダラーのhtmlで読み込むjsファイルはrequireを使う
★メインプロセス
app.on('ready', () => {
    mainWindow = new BrowserWindow({
        webPreferences: {
            nodeIntegration: true
        }
    });
});

BrowserWindowの初期化時にはwebPreferencesのnodeIntegrationをtrueに設定。

★レンダラープロセス(html)
<script>
   require('./index.js')
</script>

requireでjsファイルを読み込む

☆nodeIntegrationのデフォルト値はV5で変わったらしい

electronjs.org
ただ、ネットの情報を見る感じだと、過去にもnodeIntegrationのはコロコロ変わってるもよう