0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

electronでUncaught ReferenceError: exports is not defined at renderer.js:2

Last updated at Posted at 2020-12-20

Electron実行時に以下のエラーが出た。

Uncaught ReferenceError: exports is not defined     at renderer.js:2

レンダラープロセスでnodejsを使おうとして上記のエラーが発生した。

簡単な解決策

window作成時にnodeIntegrationを有効にする。ただし有効にするのはセキュリティ上良くない。

main.ts
  const mainWindow = new BrowserWindow({
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, "preload.js"),
      nodeIntegration: true // 追加
    },
    width: 800,
  });

index.htmlにexportsを定義する

index.html
<script> var exports = {}; </script>

noteIntegration

レンダラープロセスでnodejsの機能が使えるとxss脆弱性があった時に被害が大きくなるためデフォルトではnodejsの機能が使えなくなっている。
別の回避策はcontextBridgeを使う方法がある。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?