Electron: 'require is not defined' in the renderer; expose APIs through a preload with contextBridge
@devsingh05▲ 5 net votes
Modern Electron disables nodeIntegration and enables contextIsolation by default, so renderer code cannot use require or Node modules. Create a preload script (webPreferences.preload) that uses contextBridge.exposeInMainWorld('api', { readFile: (p) => ipcRenderer.invoke('read-file', p) }) and handle the call in the main process with ipcMain.handle. Do not turn nodeIntegration back on for pages that load remote content.