contextBridge API requirements
The api provided to exposeInMainWorld must be a Function, string, number, Array, boolean, or an object whose keys are strings and values are a Function, string, number, Array, boolean, or another nested object that meets the same conditions.
contextBridge supported types table
Type support for parameters, errors, and return values:
string: Simple type, parameter support ✅, return value support ✅, no limitations.
number: Simple type, parameter support ✅, return value support ✅, no limitations.
boolean: Simple type, parameter support ✅, return value support ✅, no limitations.
Object: Complex type, parameter support ✅, return value support ✅, limitations: keys must be supported using only simple types; values must be supported in table; prototype modifications are dropped; custom classes copy values but not prototype.
Array: Complex type, parameter support ✅, return value support ✅, same limitations as Object type.
Error: Complex type, parameter support ✅, return value support ✅, limitations: errors thrown are copied, message and stack trace may change due to being thrown in different context, custom properties on Error object will be lost.
Promise: Complex type, parameter support ✅, return value support ✅, no limitations.
Function: Complex type, parameter support ✅, return value support ✅, limitations: prototype modifications are dropped; sending classes or constructors will not work.
Cloneable Types (per MDN Web Workers API structured clone algorithm): Simple type, parameter support ✅, return value support ✅, see MDN documentation.
Element: Complex type, parameter support ✅, return value support ✅, limitations: prototype modifications are dropped; custom elements will not work.
Blob: Complex type, parameter support ✅, return value support ✅, no limitations.
VideoFrame: Complex type, parameter support ✅, return value support ✅, no limitations.
Symbol: N/A complexity, parameter support ❌, return value support ❌, symbols cannot be copied across contexts so they are dropped.
contextBridge exposing Node global symbols example
Example of using contextBridge to expose Node APIs such as crypto to the renderer:
const { contextBridge } = require('electron')
const crypto = require('node:crypto')
contextBridge.exposeInMainWorld('nodeCrypto', {
sha256sum (data) {
const hash = crypto.createHash('sha256')
hash.update(data)
return hash.digest('hex')
}
})