@electron/remote
@electron/remoteremote
remote
@electron/remote
remote@electron/remote
First, you need to install it from NPM:
@electron/remote/main
require('electron').remoterequire('@electron/remote')
remote@electron/remote@electron/remote
electron >= 14.0.0enableWebContentsrequire("@electron/remote/main").enable(webContents)
electron < 14.0.0@electron/remoteenableRemoteModule{ webPreferences: { enableRemoteModule: true } }BrowserWindow@electron/remote
API Reference
remote
dialogmenuipcremote
In order for this to work, you first need to initialize the main-process side of the remote module:
electron >= 14.0.0WebContentsWebContentsrequire("@electron/remote/main").enable(webContents)
electron < 14.0.0
Remote Objects
remote
BrowserWindowwinnew BrowserWindowBrowserWindowBrowserWindowwin
Note: Only enumerable properties which are present when the remote object is first referenced are accessible via remote.
remote
Lifetime of Remote Objects
Electron makes sure that as long as the remote object in the renderer process lives (in other words, has not been garbage collected), the corresponding object in the main process will not be released. When the remote object has been garbage collected, the corresponding object in the main process will be dereferenced.
If the remote object is leaked in the renderer process (e.g. stored in a map but never freed), the corresponding object in the main process will also be leaked, so you should be very careful not to leak remote objects.
Primary value types like strings and numbers, however, are sent by copy.
Passing callbacks to the main process
remote
First, in order to avoid deadlocks, the callbacks passed to the main process are called asynchronously. You should not expect the main process to get the return value of the passed callbacks.
Array.map
As you can see, the renderer callback's synchronous return value was not as expected, and didn't match the return value of an identical callback that lives in the main process.
Second, the callbacks passed to the main process will persist until the main process garbage-collects them.
close
But remember the callback is referenced by the main process until you explicitly uninstall it. If you do not, each time you reload your window the callback will be installed again, leaking one callback for each restart.
close
To avoid this problem, ensure you clean up any references to renderer callbacks passed to the main process. This involves cleaning up event handlers, or ensuring the main process is explicitly told to dereference callbacks that came from a renderer process that is exiting.
Accessing built-in modules in the main process
remoteelectron
Methods
remote
remote.require(module)
module
anyrequire(module)
e.g.
remote.getCurrentWindow()
BrowserWindow
removeAllListenersBrowserWindowblur
remote.getCurrentWebContents()
WebContents
remote.getGlobal(name)
name
anynameglobal[name]
Properties
remote.process
NodeJS.Processprocessremote.getGlobal('process')
Overriding exposed objects
@electron/remote@electron/remotegetGlobalrequiregetCurrentWindowgetCurrentWebContents
appWebContentsappEventWebContentspreventDefaultreturnValue
Events
Event: 'remote-require'
Returns:
eventmoduleName
remote.require()webContentsevent.preventDefault()event.returnValue
Event: 'remote-get-global'
Returns:
eventglobalName
remote.getGlobal()webContentsevent.preventDefault()event.returnValue
Event: 'remote-get-builtin'
Returns:
eventmoduleName
remote.getBuiltin()webContentsrequire("@electron/remote").BrowserWindowevent.preventDefault()event.returnValue
Event: 'remote-get-current-window'
Returns:
event
remote.getCurrentWindow()webContentsevent.preventDefault()event.returnValue
Event: 'remote-get-current-web-contents'
Returns:
event
remote.getCurrentWebContents()webContentsevent.preventDefault()event.returnValue