Electronで開発を始めようと思ったらつまづいた
Electronではレンダー側でnodeのモジュールを使う際にはセキュリティの問題から初期設定で使用不可となるよう設定しており、以前のバージョンと比べてセキュリティが向上している。
それに従って、以前までのコードでは動かなくなっている部分もあるようなのでtips
main.js
functioncreateWindow(){// Create the browser window.constwin=newBrowserWindow({width:800,height:600,})//↑これではレンダー側の処理でnodeモジュールを使用することができない。// and load the index.html of the app.win.loadFile('index.html')// Open the DevTools.win.webContents.openDevTools()}v9(2020年3月1日)以降では次のように設定を行う必要がある。
main.js(v9以降)
functioncreateWindow(){// Create the browser window.constwin=newBrowserWindow({width:800,height:600,webPreferences:{nodeIntegration:true,enableRemoteModule:true,}})//nodeIntegrationとenableRemoteModuleをtrueに// and load the index.html of the app.win.loadFile('index.html')// Open the DevTools.win.webContents.openDevTools()}nodeIntegrationを付け加えなければそもそもnodeモジュールの使用が不可。
nodeIntegrationをtrueにした場合でもenableRemoteModuleがfalseではレンダー側での.remoteが使用不可のままとなる。