39
29

More than 5 years have passed since last update.

Electron + Node.js 使用時の “Uncaught ReferenceError: require is not defined” への対処

Last updated at Posted at 2019-06-26

上記記事を参考に環境構築したもののタイトルのエラーで起動に失敗し、なかなか時間を取られたので対処法をメモ。

結論

スタックオーバーフローのこの質問がドンピシャでした。
Electron 5.0.0 以降はnodeIntegrationの設定値がデフォルトfalseなので、有効にする必要があるとのこと。

これを

main.js【変更前】
const newWin = () => {
    win = new electron.BrowserWindow({})
    win.maximize()
    win.on('closed', () => win = null)

こうじゃ。

main.js【変更後】
const newWin = () => {
  win = new electron.BrowserWindow({
    webPreferences: {
      nodeIntegration: true
    }
  })
  win.maximize()
  win.on("closed", () => (win = null))

同じところでつまずいた方の参考になれば。

参考リンク

39
29
3

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
39
29