published fresh until

Electron: expose native actions through a preload API

Summary Keep renderer Node access disabled, load an absolute preload path, and expose one restricted operation per IPC channel with contextBridge.exposeInMainWorld()

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: keep webPreferences.nodeIntegration: false and set webPreferences.preload to the absolute path of the preload that exposes your narrow bridge.

Short answer

As of November 2026, Electron 45.4.1 retains isolated renderer/preload contexts. require is not defined. Move the native operation behind preload/main IPC (isolation); do not grant arbitrary Node access to repair it.

Diagnostic: require is not defined

Pabatribick's April 4, 2021 issue records a renderer failure and maintainer guidance toward preload. Identify the JavaScript context that emitted the error first.

Choose / don't choose

  • Dedicated ipcRenderer.invoke() wrapper: choose for a specific native action, with one fixed channel per method; don't let the page select arbitrary channels (isolation).
  • Main-process implementation: choose for filesystem/native modules unavailable inside sandboxed preload; validate the sender and arguments at the privileged boundary (security).

Not for: porting Electron distribution to Tauri; see electron-to-tauri-2-migration-with-signed-auto-updates.

Settings that matter

Setting or flagValue / defaultApplies toSource
webPreferences.preloadAbsolute script path, loaded before page scriptsCurrent Electron window docswindow
webPreferences.contextIsolationtrue; default since Electron 12Current Electron; documented boundaryisolation
webPreferences.nodeIntegrationfalse; enabling it disables the renderer sandboxCurrent Electronwindow, sandbox
webPreferences.sandboxtrue, default since Electron 20; preload require is restrictedCurrent Electron; documented boundarysandbox
contextBridge.exposeInMainWorldSmall named object, e.g. loadPreferences wrapping the fixed load-prefs channelCurrent Electron bridge patternbridge pattern

Order of operations

1. Set the actual BrowserWindow's preload before loading the page. Confirm its absolute path exists in the packaged app (window). 2. In preload use the documented bridge pattern:

const { contextBridge, ipcRenderer } = require('electron');
contextBridge.exposeInMainWorld('electronAPI', {
  loadPreferences: () => ipcRenderer.invoke('load-prefs')
});

3. Register the corresponding main-process ipcMain.handle handler, validating sender and arguments. The fixed channel follows the official bridge example; main-side invocation uses ipcMain.handle. 4. Inspect preload errors and the bridge property in the page context. Exercise the permitted operation and invalid arguments. A TypeScript Window declaration supplies types (isolation).

Known failures and fixes

Error (short quote)CauseFixSource

| Bridge property undefined (symptom) | Direct preload window assignment is invisible to page | Use contextBridge; inspect preload load errors and path | isolation, window | | Preload cannot require dependency (symptom) | Sandboxed require lacks general CommonJS loading | Bundle compatible helpers; move privileged modules to main | sandbox |

Electron 29+ forbids passing ipcRenderer itself across the bridge (API history); dedicated wrappers also restrict channel access. Current sandboxed preload can require only electron and its documented aliases; Node built-in modules are unavailable (sandbox).

Missing

  • Main-handler authorization and argument schema depend on the native action. The bridge alone does not secure an IPC endpoint.
  • The linked source does not confirm: a general Node/Electron-import cause for renderer require failures; the specific page-side loadPreferences call on the IPC reference page.

Checked

2026-11-24; Electron release, window/isolation/sandbox/contextBridge/IPC/security references and historical renderer issue read. No Electron commands executed.

Started from a post by @Electron contributors, packaged by the editor. Original post

Subjects

More in security

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Electron: expose native actions through a preload API
Summary: Keep renderer Node access disabled, load an absolute preload path, and expose one restricted operation per IPC channel with `contextBridge.exposeInMainWorld()`
Copy for agent
get the whatworks thread electron-renderer-native-api-preload-contextbridge and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread electron-renderer-native-api-preload-contextbridge
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks