Nuxt Electron
Integrate Nuxt and Electron
Features
- 📦 Out of the box
- 🔥 Hot restart (Main process)
- 🚀 Hot reload (Preload script)
Quick Setup
- Add the following dependency to your project
pnpm add -D nuxt-electron vite-plugin-electron vite-plugin-electron-renderer electron electron-builder
yarn add --dev nuxt-electron vite-plugin-electron vite-plugin-electron-renderer electron electron-builder
npm install --save-dev nuxt-electron vite-plugin-electron vite-plugin-electron-renderer electron electron-builder
- Add
nuxt-electron
to the modules
section of nuxt.config.ts
export default defineNuxtConfig({
modules: ['nuxt-electron'],
electron: {
build: [
{
entry: 'electron/main.ts',
},
],
},
})
- Create the
electron/main.ts
file and type the following code
import { app, BrowserWindow } from 'electron'
app.whenReady().then(() => {
new BrowserWindow().loadURL(process.env.VITE_DEV_SERVER_URL)
})
- Add the
main
entry to package.json
{
+ "main": "dist-electron/main.js"
}
That's it! You can now use Electron in your Nuxt app ✨
Electron Options
This is based on the vite-plugin-electron
, see the Documents for more detailed options
export interface ElectronOptions {
build: import('vite-plugin-electron').ElectronOptions[],
renderer?: Parameters<typeof import('vite-plugin-electron-renderer').default>[0]
disableDefaultOptions?: boolean
}
Recommend Structure
Let's use the official nuxt-starter-v3 template as an example
+ ├─┬ electron
+ │ └── main.ts
├─┬ public
│ └── favicon.ico
├── .gitignore
├── .npmrc
├── index.html
├── app.vue
├── nuxt.config.ts
├── package.json
├── README.md
└── tsconfig.json
Notes
By default, we force the App to run in SPA mode since we don't need SSR for desktop apps
If you want to fully customize the default behavior, you can disable it by disableDefaultOptions
TODO