Search code examples
javascriptbrowserelectronrequire

How is it legitimate to use require in Electron's client when no there is no explicit import of any require framework?


To communicate with the main process in Electron, the renderer process requires the ipcMain module ( https://github.com/electron/electron/blob/master/docs/api/ipc-main.md ):

<script>
  // In renderer process (web page).
  const {ipcMain} = require('electron')
</script>

I'm puzzled as to why this is legitimate. In the client html file which contains this script there is no reference to any require framework ( there is no script tag to import any require framework ). Yet out of thin air you can use require, which is not a Javascript keyword.

How does the require mechanism work in Electron?


Solution

  • Electron's renderer process is not plain chromium process - when Electron launches renderer window, it internally inject global object enables Electron specific features. require is one of them, patching global to expose node.js module resolution in chromium process.