# @electron-toolkit/preload

> Toolkit for electron preload scripts.

Latest version **3.0.2** (published 2025-04-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install @electron-toolkit/preload
pnpm add @electron-toolkit/preload
yarn add @electron-toolkit/preload
bun add @electron-toolkit/preload
```

## Health

**Score 45/100 (D)** — status: stable.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 3.0.2 |
| Published | 2025-04-19 |
| First published | 2022-03-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 18.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 216 |
| Author | Alex Wei |
| Maintainers | alex.wei |
| Keywords | electron, toolkit, preload, ipcRenderer, webFrame |

## Links

- npm: https://www.npmjs.com/package/@electron-toolkit/preload
- Repository: https://github.com/alex8088/electron-toolkit
- Homepage: https://github.com/alex8088/electron-toolkit/tree/master/packages/preload#readme
- Issues: https://github.com/alex8088/electron-toolkit/issues
- npm.io page: https://npm.io/package/@electron-toolkit/preload

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 3.0.2 (latest) — 2025-04-19
- 3.0.1 — 2024-04-04
- 3.0.0 — 2023-12-19
- 2.0.0 — 2023-06-04
- 1.0.3 — 2022-12-28
- 1.0.2 — 2022-04-23
- 1.0.1 — 2022-03-28
- 1.0.0 — 2022-03-28

## README

# @electron-toolkit/preload

> Easy to expose Electron APIs (ipcRenderer,webFrame,webUtils,process) to renderer.

---

## Usage

### Install

```sh
npm i @electron-toolkit/preload
```

### Get Started

First, use `contextBridge` APIs to expose Electron APIs to renderer only if context isolation is enabled, otherwise just add to the DOM global.

```javascript
import { contextBridge } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
  } catch (error) {
    console.error(error)
  }
} else {
  window.electron = electronAPI
}
```

or

```javascript
import { exposeElectronAPI } from '@electron-toolkit/preload'

exposeElectronAPI()
```

Then, use the Electron APIs directly in the renderer process：

```javascript
// Send a message to the main process with no response
window.electron.ipcRenderer.send('electron:say', 'hello')

// Send a message to the main process with the response asynchronously
window.electron.ipcRenderer.invoke('electron:doAThing', '').then(re => {
  console.log(re)
})

// Receive messages from the main process
window.electron.ipcRenderer.on('electron:reply', (_, args) => {
  console.log(args)
})

// Remove a listener
const removeListener = window.electron.ipcRenderer.on('electron:reply', (_, args) => {})
removeListener()
```

**Note**: If you're building your Electron app with TypeScript, you may want to get TypeScript intelliSense for the renderer process. So that you can create a `*.d.ts` declaration file and globally augment the `Window` interface:

```javascript
import { ElectronAPI } from '@electron-toolkit/preload'

declare global {
  interface Window {
    electron: ElectronAPI
  }
}
```

## API

### IpcRenderer

- `send`
- `sendTo`
- `sendSync`
- `sendToHost`
- `invoke`
- `postMessage`
- `on`
- `once`
- `removeAllListeners`
- `removeListener`

### WebFrame

- `insertCSS`
- `setZoomFactor`
- `setZoomLevel`

### WebUtils

- `getPathForFile`

### NodeProcess

- `platform` property
- `versions` property
- `env` property

## License

[MIT](./LICENSE) © alex.wei

---
_Source: https://npm.io/package/@electron-toolkit/preload · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
