Nuxt 2 is reaching End-of-Life on June 30th, 2024.

electron
electron

Integrate Nuxt and Electron.

Nuxt Electron

npm versionnpm downloadsLicense

Integrate Nuxt and Electron

Features

  • ๐Ÿ“ฆ Out of the box
  • ๐Ÿ”ฅ Hot restart (Main process)
  • ๐Ÿš€ Hot reload (Preload script)

Quick Setup

  1. Add the following dependency to your project
# Using pnpm
pnpm add -D nuxt-electron vite-plugin-electron vite-plugin-electron-renderer electron electron-builder
# Using yarn
yarn add --dev nuxt-electron vite-plugin-electron vite-plugin-electron-renderer electron electron-builder
# Using npm
npm install --save-dev nuxt-electron vite-plugin-electron vite-plugin-electron-renderer electron electron-builder
  1. Add nuxt-electron to the modules section of nuxt.config.ts
export default defineNuxtConfig({
  modules: ['nuxt-electron'],
  electron: {
    build: [
      {
        // Main-Process entry file of the Electron App.
        entry: 'electron/main.ts',
      },
    ],
  },
})
  1. 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)
})
  1. 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` can specify multiple entry builds, which can be Main process, Preload scripts, Worker process, etc.
   * 
   * @example
   * 
   * ```js
   * export default defineNuxtConfig({
   *   modules: ['nuxt-electron'],
   *   electron: {
   *     build: [
   *       {
   *         // Main-Process entry file of the Electron App.
   *         entry: 'electron/main.ts',
   *       },
   *     ],
   *   },
   * })
   * ```
   */
  build: import('vite-plugin-electron').ElectronOptions[],
  /**
   * @see https://github.com/electron-vite/vite-plugin-electron-renderer
   */
  renderer?: Parameters<typeof import('vite-plugin-electron-renderer').default>[0]
  /**
   * nuxt-electron will modify some options by default
   * 
   * @defaultValue
   * 
   * ```js
   * export default defineNuxtConfig({
   *   ssr: false,
   *   app: {
   *     baseURL: './',
   *     buildAssetsDir: '/',
   *   },
   *   runtimeConfig: {
   *     app: {
   *       baseURL: './',
   *       buildAssetsDir: '/',
   *     },
   *   },
   *   nitro: {
   *     runtimeConfig: {
   *       app: {
   *         baseURL: './,
   *       }
  *      }
   *   },
   * })
   * ```
   */
  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

Examples

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

  • write test