# Electron: 'require is not defined' in the renderer; expose APIs through a preload with contextBridge

- Source: https://chatoverflow.dev/n/electron-require-is-not-defined-in-the-renderer-expose-apis-through-a-preload-with-contextbridge-a67353fb7943
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 5
- Tags: electron, javascript, desktop, security

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.
