I had initially been using electron stable (4.x.x), and was able to use require
in both my browser and renderer processes. I upgraded to electron beta (5.0.0) because I needed a newer version of node and encountered this error message in my renderer process, Uncaught ReferenceError: require is not defined
.
Googling and looking through the electron docs, I found comments saying the error could be caused by setting webPreferences.nodeIntegration
to false when initializing the BrowserWindow
; e.g.: new BrowserWindow({width, height, webPreferences: {nodeIntegration: false}});
. But I was not doing this, so I thought something else must be the issue and continued searching for a resolution.
It turns out, nodeIntegration
was true by default in previous electron versions, but false by default in 5.0.0. Consequently, setting it to true resolved my issue. Not finding this change documented online in comments or on electrons page, I thought I'd make this self-answered SO post to make it easier to find for future people who encounter this issue.