LoginSignup
0
2

More than 3 years have passed since last update.

Electron(v9以降)でrequire("hoge").remoteを使えない時のためのtips

Last updated at Posted at 2020-10-03

Electronで開発を始めようと思ったらつまづいた

Electronではレンダー側でnodeのモジュールを使う際にはセキュリティの問題から初期設定で使用不可となるよう設定しており、以前のバージョンと比べてセキュリティが向上している。

それに従って、以前までのコードでは動かなくなっている部分もあるようなのでtips

main.js
function createWindow () {
  // Create the browser window.
  const win = new BrowserWindow({
    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以降)
function createWindow () {
  // Create the browser window.
  const win = new BrowserWindow({
    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が使用不可のままとなる。

thank you stackoverflow

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2