Electron Vite Build Tooling
electron-vite is a specialized build toolchain for Electron applications, leveraging the fast development experience of Vite. It streamlines the development process for Electron's main, preload, and renderer processes by providing pre-configured setups and a unified configuration API. The current stable version is 5.0.0, with beta releases for version 6.0.0 actively under development, indicating a moderate to rapid release cadence for new features and improvements. Key differentiators include its tight integration with Vite for all three Electron process types, simplified configuration, and out-of-the-box support for TypeScript and modern web development practices, making it a powerful alternative to more manual or less integrated build setups.
Common errors
-
Error: The installed Node.js version (vX.Y.Z) does not meet electron-vite's requirements (^20.19.0 || >=22.12.0).
cause Your current Node.js version is incompatible with the installed electron-vite package.fixUpgrade your Node.js environment to a version within the specified range, e.g., `nvm install 20.19.0 && nvm use 20.19.0`. -
Error: Cannot find module 'vite' or 'vite/package.json' from 'path/to/node_modules/electron-vite'
cause `vite` is a peer dependency and must be explicitly installed in your project's `devDependencies`.fixInstall Vite by running `npm install -D vite@latest` or `yarn add -D vite@latest` in your project root. -
TypeError: (0, electron_vite_1.defineConfig) is not a function
cause This error often indicates that `defineConfig` was not correctly imported from `electron-vite`, or there's a module resolution issue, potentially due to an incorrect import path or an older `electron-vite` version.fixEnsure your import statement is `import { defineConfig } from 'electron-vite'` and verify that `electron-vite` is correctly installed. Clear `node_modules` and reinstall if necessary. -
Error: @swc/core is a peer dependency and needs to be installed.
cause `@swc/core` is a required peer dependency for fast transpilation, and it is missing from your project's `devDependencies`.fixInstall `@swc/core` by running `npm install -D @swc/core@latest` or `yarn add -D @swc/core@latest`.
Warnings
- breaking Node.js engine requirements have been updated. Your environment must meet `^20.19.0 || >=22.12.0` to ensure compatibility and prevent build failures or unexpected runtime issues.
- gotcha `electron-vite` relies on `vite` and `@swc/core` as peer dependencies. These packages must be explicitly installed in your project at compatible versions; otherwise, you may encounter warnings or build errors.
- breaking Major version changes (e.g., from v4 to v5, and upcoming v6) typically introduce breaking changes to the configuration API, internal build processes, or required project structure. Direct upgrades without checking may lead to errors.
- gotcha Incorrect path resolution within the `electron.vite.config.ts` can cause modules to be unresolvable during the build process, especially for entry points or static assets. Relative paths can be ambiguous.
Install
-
npm install electron-vite -
yarn add electron-vite -
pnpm add electron-vite
Imports
- defineConfig
import { defineConfig } from '@electron-vite/config'import { defineConfig } from 'electron-vite' - resolve
import { resolve } from 'path' - UserConfig
import { UserConfig } from 'electron-vite'
Quickstart
import { defineConfig } from 'electron-vite'
import { resolve } from 'path'
export default defineConfig({
main: {
// Main process build configuration
build: {
lib: {
entry: 'src/main/index.ts',
formats: ['cjs']
},
outDir: 'dist/main',
rollupOptions: {
external: ['electron'] // Prevent bundling electron itself
}
}
},
preload: {
// Preload script build configuration
build: {
lib: {
entry: 'src/preload/index.ts',
formats: ['cjs']
},
outDir: 'dist/preload',
rollupOptions: {
external: ['electron']
}
}
},
renderer: {
// Renderer process build configuration
root: '.', // Relative to electron.vite.config.ts
build: {
outDir: 'dist/renderer',
rollupOptions: {
// Example: externalize some renderer dependencies if needed
external: []
}
}
}
})