Now liveThe Skillselion MCP - thousands of ranked skills, loaded into your agent mid-task. No install.Get it →
jwynia avatar

Electron Best Practices

  • 734 installs
  • 133 repo stars
  • Updated February 24, 2026
  • jwynia/agent-skills

electron-best-practices is an agent skill at version 1.0 that guides secure Electron plus React development with electron-vite, type-safe IPC, packaging, and Playwright testing for developers shipping desktop application

About

electron-best-practices is a version 1.0 agent skill guiding secure Electron 20+ desktop app development with React, electron-vite, and Electron Forge packaging. The skill enforces context isolation, sandbox mode, and disabled nodeIntegration, routing all main-renderer communication through contextBridge invoke/handle patterns with typed IpcChannelMap definitions. Bundled asset templates cover electron.vite.config.ts, forge.config.js, Playwright config, typed IPC handlers, preload scripts, and multi-window examples. Developers reach for electron-best-practices when scaffolding production desktop apps requiring code signing, notarization, CSP headers, and Playwright end-to-end tests. The skill explicitly excludes Tauri projects and Electron versions below 20 where security defaults differ.

  • Unified Vite config for all three Electron processes in one file
  • Automatic externalizeDepsPlugin usage for main and preload
  • React plugin + proper HTML entry for the renderer process
  • Cross-process path aliasing with @shared resolution
  • Environment-aware minification, sourcemaps, and rollupOptions

Electron Best Practices by the numbers

  • 734 all-time installs (skills.sh)
  • +13 installs in the week ending Aug 2, 2026 (Skillselion tracking)
  • Ranked #473 of 2,245 Frontend Development skills by installs in the Skillselion catalog
  • Security screen: MEDIUM risk (skills.sh audit)
  • Data as of Aug 5, 2026 (Skillselion catalog sync)
npx skills add https://github.com/jwynia/agent-skills --skill electron-best-practices

Add your badge

Show developers this skill is listed on Skillselion. Paste this into your README.

Listed on Skillselion
Installs734
repo stars133
Security audit3 / 3 scanners passed
Last updatedFebruary 24, 2026
Repositoryjwynia/agent-skills

How do you configure secure Electron React IPC?

Generate production-grade electron-vite configurations that correctly wire main, preload, and renderer processes with React, path aliases, and environment-aware o

Who is it for?

Developers building production Electron 20+ desktop apps with React who need secure IPC, electron-vite wiring, and packaging guidance in agent workflows.

Skip if: Teams building Tauri or pure web apps with no desktop packaging, signing, or multi-process Electron requirements.

When should I use this skill?

A developer asks to set up electron-vite, configure contextBridge IPC, package Electron apps, or implement Electron security patterns with React.

What you get

electron.vite.config.ts, typed preload contextBridge API, IPC handler map, Forge packaging config, and Playwright test setup.

  • electron.vite.config.ts
  • typed preload script
  • Forge packaging configuration

By the numbers

  • Skill version 1.0 with bundled config and template asset files
  • Targets Electron 20+ security defaults across three process types
  • Covers main, preload, and renderer process configuration patterns

Files

SKILL.mdMarkdownGitHub ↗

Electron + React Best Practices

Guide AI agents in building secure, production-ready Electron applications with React. This skill provides security patterns, type-safe IPC communication, project setup guidance, packaging and code signing workflows, and tools for analysis, scaffolding, and type generation.

When to Use This Skill

Use this skill when:

  • Generating Electron main, preload, or renderer process code
  • Configuring electron-vite or Electron Forge
  • Setting up IPC communication between processes
  • Implementing security patterns (contextBridge, sandbox, CSP)
  • Packaging, signing, and notarizing desktop applications
  • Testing Electron apps with Playwright
  • Designing multi-window architectures

Do NOT use this skill when:

  • Building Tauri apps (different paradigm, use Tauri-specific guidance)
  • Building pure web apps with no desktop requirements
  • Targeting Electron versions below 20 (security defaults differ)
  • Using non-React renderer frameworks (use framework-specific skills)

Core Principles

1. Security First Architecture

Modern Electron security relies on three defaults that became standard in Electron 20+: context isolation, sandbox mode, and nodeIntegration disabled. Disabling any of them allows XSS attacks to escalate to full remote code execution. All main-renderer communication must flow through contextBridge:

// preload.ts - SECURE pattern
contextBridge.exposeInMainWorld('electronAPI', {
  loadPreferences: () => ipcRenderer.invoke('load-prefs'),
  saveFile: (content: string) => ipcRenderer.invoke('save-file', content),
  onUpdateCounter: (callback: (value: number) => void) => {
    const handler = (_event: IpcRendererEvent, value: number) => callback(value);
    ipcRenderer.on('update-counter', handler);
    return () => ipcRenderer.removeListener('update-counter', handler);
  }
});

Set Content Security Policy via HTTP headers for apps loading local files, restricting script sources to 'self'.

2. Type-Safe IPC Communication

The invoke/handle pattern is preferred over send/on for request-response communication, providing proper async/await semantics and error propagation. For typed channels, use a mapped type pattern:

type IpcChannelMap = {
  'load-prefs': { args: []; return: UserPreferences };
  'save-file': { args: [content: string]; return: { success: boolean } };
};

For complex applications, electron-trpc provides full type safety using tRPC's router pattern with Zod validation:

export const appRouter = t.router({
  greeting: t.procedure
    .input(z.object({ name: z.string() }))
    .query(({ input }) => `Hello, ${input.name}!`),
});

Error handling across the IPC boundary requires attention because Electron only serializes the message property of Error objects. Wrap responses in a { success, data, error } result type to preserve full error context.

3. Modern Project Setup

The recommended stack uses electron-vite for development and Electron Forge for packaging. electron-vite provides a unified configuration managing main, preload, and renderer processes with sub-second dev server startup and instant HMR. Electron Forge uses first-party Electron packages for signing and notarization.

src/
├── main/           # Main process (Node.js environment)
│   ├── index.ts
│   └── ipc/        # IPC handlers
├── preload/        # Secure bridge via contextBridge
│   ├── index.ts
│   └── index.d.ts  # TypeScript declarations for exposed APIs
└── renderer/       # React application (pure web, no Node access)
    ├── src/
    └── index.html

4. React Integration Patterns

React 18's concurrent features work normally in Electron's Chromium-based renderer. Strict Mode's double-invocation of effects catches IPC listener leaks that would otherwise cause memory issues. Always return cleanup functions from effects that register IPC listeners:

useEffect(() => {
  const cleanup = window.electronAPI.onUpdateCounter((value) => {
    setCount(value);
  });
  return cleanup;
}, []);

For multi-window applications, the main process should serve as the single source of truth for shared state. Use electron-store for persistence combined with IPC broadcasting so any window's mutation updates all others.

Quick Reference

CategoryPreferAvoid
SecuritycontextBridge.exposeInMainWorld()nodeIntegration: true
IPCinvoke/handle patternsend/on for request-response
PreloadTyped function wrappersExposing raw ipcRenderer
Build toolelectron-vitewebpack-based toolchains
PackagingElectron ForgeManual packaging
StateZustand + electron-storeRedux for simple apps
TestingPlaywright E2ESpectron (deprecated)
Updateselectron-updaterManual update checks
SigningCI-integrated code signingUnsigned releases
CSPHTTP headers, 'self' onlyNo CSP
Error handlingResult type {success, data, error}Raw Error across IPC
Multi-windowMain process as state hubDirect window-to-window

Code Generation Guidelines

When generating Electron code, follow these patterns:

BrowserWindow Creation

const win = new BrowserWindow({
  webPreferences: {
    preload: path.join(__dirname, '../preload/index.js'),
    contextIsolation: true,
    sandbox: true,
    nodeIntegration: false,
  },
});

Always enable contextIsolation and sandbox. Never enable nodeIntegration. The preload path must resolve to the built output location.

IPC Handler Module

export function registerFileHandlers(): void {
  ipcMain.handle('save-file', async (_event, content: string) => {
    try {
      await fs.writeFile(filePath, content);
      return { success: true, data: filePath };
    } catch (err) {
      return { success: false, error: (err as Error).message };
    }
  });
}

Group related handlers into modules. Use the result type pattern for all return values. Validate all arguments received from the renderer process.

Common Anti-Patterns

Avoid these patterns when generating Electron code:

Anti-PatternProblemSolution
nodeIntegration: trueXSS escalates to full RCEKeep disabled (default)
Exposing ipcRenderer directlyFull IPC access from rendererWrap in contextBridge functions
Missing contextIsolationRenderer accesses preload scopeKeep enabled (default since Electron 12)
No code signingOS security warnings, Gatekeeper blocksSign and notarize for all platforms
BrowserWindow without sandboxPreload has full Node.js accessEnable sandbox (default since Electron 20)
Unvalidated IPC argumentsInjection attacks from rendererValidate with Zod or manual checks
0.0.0.0 server bindingNetwork-exposed local serverAlways bind to 127.0.0.1
Missing CSP headersScript injection vectorsSet strict CSP via HTTP headers
No IPC error serializationLost error context across boundaryUse Result type pattern
Spectron for testingDeprecated, Electron 13 maxUse Playwright

See references/security/security-checklist.md for the full security audit checklist.

Scripts Reference

analyze-security.ts

Analyze Electron projects for security misconfigurations:

deno run --allow-read scripts/analyze-security.ts <path> [options]

Options:
  --strict    Enable all checks
  --json      Output JSON for CI
  -h, --help  Show help

Examples:
  # Analyze a project
  deno run --allow-read scripts/analyze-security.ts ./src

  # Strict mode for CI pipeline
  deno run --allow-read scripts/analyze-security.ts ./src --strict --json

scaffold-electron-app.ts

Scaffold a new Electron + React project with secure defaults:

deno run --allow-read --allow-write scripts/scaffold-electron-app.ts [options]

Options:
  --name <name>     App name (required)
  --path <path>     Target directory (default: ./)
  --with-react      Include React setup
  --with-trpc       Include electron-trpc
  --with-tests      Include Playwright tests

Examples:
  # Basic app with React
  deno run --allow-read --allow-write scripts/scaffold-electron-app.ts \
    --name "my-app" --with-react

  # Full setup with trpc and tests
  deno run --allow-read --allow-write scripts/scaffold-electron-app.ts \
    --name "my-app" --with-react --with-trpc --with-tests

generate-ipc-types.ts

Generate TypeScript type definitions from IPC handler files:

deno run --allow-read --allow-write scripts/generate-ipc-types.ts [options]

Options:
  --handlers <path>  Path to IPC handler files
  --output <path>    Output path for type definitions
  --validate         Validate existing types match handlers

Examples:
  # Generate types from handlers
  deno run --allow-read --allow-write scripts/generate-ipc-types.ts \
    --handlers ./src/main/ipc --output ./src/preload/ipc-types.d.ts

  # Validate types in CI
  deno run --allow-read scripts/generate-ipc-types.ts \
    --handlers ./src/main/ipc --validate

Additional Resources

Security

  • references/security/context-isolation.md - contextBridge and isolation patterns
  • references/security/csp-and-permissions.md - Content Security Policy configuration
  • references/security/security-checklist.md - Full security audit checklist

IPC Communication

  • references/ipc/typed-ipc.md - Typed channel map patterns
  • references/ipc/electron-trpc.md - tRPC integration for full type safety
  • references/ipc/error-serialization.md - Result types across IPC boundary

Architecture

  • references/architecture/project-structure.md - Directory organization
  • references/architecture/process-separation.md - Main, preload, and renderer roles
  • references/architecture/multi-window-state.md - Shared state across windows

React Integration

  • references/integration/react-patterns.md - useEffect cleanup, Strict Mode
  • references/integration/state-management.md - Zustand and electron-store patterns

Packaging & Distribution

  • references/packaging/code-signing.md - Platform-specific signing workflows
  • references/packaging/auto-updates.md - electron-updater configuration
  • references/packaging/bundle-optimization.md - Size reduction techniques
  • references/packaging/ci-cd-patterns.md - GitHub Actions matrix builds

Testing

  • references/testing/playwright-e2e.md - Playwright Electron support
  • references/testing/unit-testing.md - Jest/Vitest multi-project configuration
  • references/testing/test-structure.md - Test organization patterns

Tooling

  • references/tooling/electron-vite.md - Build tool configuration
  • references/tooling/electron-forge.md - Packaging and distribution
  • references/tooling/tauri-comparison.md - When to choose Tauri instead

Templates

  • assets/templates/main-process.ts.md - Main process starter template
  • assets/templates/preload-script.ts.md - Preload script with contextBridge
  • assets/templates/ipc-handler.ts.md - IPC handler module template
  • assets/templates/react-root.tsx.md - React root component template

Configuration Examples

  • assets/configs/electron-vite.config.ts.md - electron-vite configuration
  • assets/configs/forge.config.js.md - Electron Forge configuration
  • assets/configs/tsconfig.json.md - TypeScript configuration presets
  • assets/configs/playwright.config.ts.md - Playwright Electron test config

Complete Examples

  • assets/examples/typed-ipc-example.md - End-to-end typed IPC walkthrough
  • assets/examples/multi-window-example.md - Multi-window state management

Related skills

How it compares

Pick electron-best-practices over generic React skills when Electron-specific IPC security, electron-vite builds, and desktop packaging are the primary concerns.

FAQ

What Electron version does electron-best-practices target?

electron-best-practices targets Electron 20 and above where context isolation, sandbox mode, and disabled nodeIntegration are standard security defaults. Versions below 20 are explicitly out of scope.

What IPC pattern does electron-best-practices recommend?

electron-best-practices recommends invoke/handle over send/on for request-response IPC, exposing APIs through contextBridge with typed IpcChannelMap channels and success/data/error result wrappers.

Is Electron Best Practices safe to install?

skills.sh reports 3 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.

Frontend Developmentfrontendintegrations

This week in AI coding

Five minutes, every Monday - the tools, releases and tactics for developers.

unsubscribe anytime.