{"id":20665,"library":"vite-plugin-electron","title":"vite-plugin-electron","description":"A Vite plugin that integrates Electron main process and preload scripts into the Vite build pipeline, supporting hot reload for main process via electron-rebuild (HMR). Current stable version is 0.29.1. The plugin automatically starts Electron after Vite server is ready and allows configuring entry files, build options, and external dependencies. Actively maintained with frequent releases. Key differentiator: full HMR support for Electron main process, unlike alternatives like electron-vite which require separate builds.","status":"active","version":"0.29.1","language":"javascript","source_language":"en","source_url":"https://github.com/electron-vite/vite-plugin-electron","tags":["javascript","vite","plugin","electron","renderer","typescript"],"install":[{"cmd":"npm install vite-plugin-electron","lang":"bash","label":"npm"},{"cmd":"yarn add vite-plugin-electron","lang":"bash","label":"yarn"},{"cmd":"pnpm add vite-plugin-electron","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Needed for renderer process integration; peer dependency marked as optional in later versions.","package":"vite-plugin-electron-renderer","optional":true}],"imports":[{"note":"ESM-only since v0.28; default export is the plugin factory function.","wrong":"const vitePluginElectron = require('vite-plugin-electron')","symbol":"vitePluginElectron","correct":"import vitePluginElectron from 'vite-plugin-electron'"},{"note":"The plugin re-exports defineConfig for convenience; using vite's own defineConfig also works but may miss plugin-specific types.","wrong":"import { defineConfig } from 'vite'","symbol":"defineConfig","correct":"import { defineConfig } from 'vite-plugin-electron'"},{"note":"Type-only import to avoid runtime side effects; recommended for TypeScript users.","wrong":"import { VitePluginElectronOptions } from 'vite-plugin-electron'","symbol":"type VitePluginElectronOptions","correct":"import type { VitePluginElectronOptions } from 'vite-plugin-electron'"}],"quickstart":{"code":"import { defineConfig } from 'vite';\nimport electron from 'vite-plugin-electron';\nimport renderer from 'vite-plugin-electron-renderer';\nimport path from 'path';\n\nexport default defineConfig({\n  plugins: [\n    electron([\n      {\n        entry: 'electron/main.ts',\n        onstart(args) {\n          args.startup();\n        },\n        vite: {\n          build: {\n            outDir: 'dist-electron',\n            rollupOptions: {\n              external: ['electron'],\n            },\n          },\n        },\n      },\n      {\n        entry: 'electron/preload.ts',\n        onstart(args) {\n          args.reload();\n        },\n        vite: {\n          build: {\n            outDir: 'dist-electron',\n          },\n        },\n      },\n    ]),\n    renderer(),\n  ],\n  server: {\n    port: 5173,\n  },\n});","lang":"typescript","description":"Configures Vite to build Electron main process and preload scripts with HMR support, using two plugin entries."},"warnings":[{"fix":"Upgrade to Vite 8 or stay on vite-plugin-electron <1.0.0-beta.1","message":"v1.0.0-beta.1 drops Vite 7 support and requires Vite 8.","severity":"breaking","affected_versions":">=1.0.0-beta.1"},{"fix":"Use import syntax or dynamic import() from CJS code.","message":"In v0.28, the plugin switched to ESM-only; CommonJS require() no longer works.","severity":"breaking","affected_versions":">=0.28.0"},{"fix":"Use array configuration with 'entry' and 'onstart' fields.","message":"The 'main' and 'preload' options in plugin config are deprecated in favor of an array of entry objects.","severity":"deprecated","affected_versions":">=0.28.0"},{"fix":"Call args.startup() in onstart callback to ensure proper process management.","message":"Hot reload may not work on Windows if the Electron process exits prematurely; the process must be started via args.startup().","severity":"gotcha","affected_versions":">=0.28.0"},{"fix":"Add 'electron' and other native modules to external in the Vite build config.","message":"If external dependencies are not listed in rollupOptions.external, they may be bundled incorrectly causing runtime errors.","severity":"gotcha","affected_versions":">=0.28.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Add 'electron' to rollupOptions.external in the plugin's vite config.","cause":"Electron is not listed as external in the Vite build config for main process.","error":"Error: Cannot find module 'electron'"},{"fix":"Upgrade to >=0.28 and ensure onstart receives args object with startup method.","cause":"Using an older version of the plugin (<0.28) or incorrect onstart signature.","error":"Error: startup is not a function"},{"fix":"Add 'builtinModules' from 'module' module to external in rollupOptions.","cause":"Default Vite config does not mark Node built-ins as external for Electron main process.","error":"Module not found: Error: Can't resolve 'fs'"},{"fix":"Use args.startup() to manage lifecycle; consider using the 'onstart' callback to gracefully shut down.","cause":"Process is not properly killed on Vite server shutdown.","error":"Electron app does not close on Ctrl+C in terminal"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}