Electron で開発していて、レンダラープロセスからメインのモジュールを使いたい時がありました。これから、そのときに調べた使用方法をまとめていきます。
方法
remote
を使うと、メインプロセスのモジュールを呼び出すことができます。
レンダラーのコード例
const {app, shell} = require('electron').remote; const path = require('path'); const fs = require("fs"); const file = path.join( app.getPath('userData'), 'feed.json' );
上のように remote
の app
を取得してから、app.getPath(...
(メインプロセスのAPI)を使います。下のように remote
を使わないと、メインのモジュールを使うときにエラーになったりします。
const {app, shell} = require('electron');