nextjs-electron
v0.1.3
Published
A modular Electron library to bootstrap Next.js static exports into secure, production-ready desktop apps.
Downloads
13
Maintainers
Readme
nextjs-electron
A clean, framework-agnostic, and highly modular Electron library to bootstrap your Next.js static exports (from the /out directory) into secure, production-ready desktop applications.
This module is lightweight, UI-agnostic, designed for Windows Store (MSIX) compatibility, and fully prepared for npm publishing. It offers both a powerful CLI for automatic project setup and core app generation, and a flexible library for manual integration.
Features
- 🚀 Automatic Next.js Project Setup: Use the
setup-projectCLI command to instantly integrate Electron into your existing Next.js project. - ⚙️ Core Electron App Generator: Scaffold a minimal, no-UI Electron app for background tasks or high-resource operations with the
generate-coreCLI command. - 📦 Static Export Bootstrapping: Seamlessly loads your Next.js static
/outdirectory. - 🛡️ Security by Default: Enforces context isolation, disables Node.js integration in renderers by default, provides strict IPC, and configurable CSP.
- 🖥️ Cross-Platform Packaging: Easily generate installers for Windows (MSIX), macOS, and Linux using
electron-builder, guided by CLI configurations. - 🧩 Modular & Extensible Library: For manual control, offers a clean architecture: main process controller, secure preload script, and type-safe IPC.
- ☁️ Integrated Auto-Updates: Keep your application current with built-in support for
electron-updater. - ⚡ WebGPU Support (Optional): Opt-in WebGPU capabilities for high-performance graphics or compute tasks.
- ⚖️ NPM Optimized & Lightweight: Published to NPM with CJS/ESM support and TypeScript declarations.
Installation
First, install nextjs-electron along with its peer dependencies electron and electron-builder (for packaging):
npm install nextjs-electron electron electron-builder
# or
yarn add nextjs-electron electron electron-builderGetting Started
Choose the path that best suits your needs:
Option 1: Automatic Setup for Existing Next.js Project (Recommended for Quick Integration)
If you have an existing Next.js project (configured for static export to an /out directory), this is the fastest way to get started.
Navigate to your Next.js project root directory.
Run the
setup-projectcommand:npx nextjs-electron setup-projectThe CLI will prompt you for:
- Your application name (e.g., "My Awesome App")
- Your application ID (e.g., "com.mycompany.myawesomeapp")
It will then automatically:
- Add/update
electron,electron-builder, andnextjs-electronin yourpackage.json. - Set the
mainfield inpackage.jsontoelectron-main.js. - Add
start:electronandpackage:electronscripts to yourpackage.json. - Create a basic
electron-main.jsfile in your project root, pre-configured with your app name and ID.
Install new dependencies:
npm install # or yarn installBuild your Next.js app (if you haven't already): Make sure your
next.config.jshasoutput: 'export'.npx next buildLaunch your Electron app:
npm run start:electronFor packaging, consider initializing a custom builder config:
npx nextjs-electron init-configThen package using:
npm run package:electron
Option 2: Generate a Minimal Core Electron App (for Background Tasks)
If you need a new, standalone Electron application without a UI (e.g., for backend processing, automation tasks, or high-resource computations), use the generate-core command.
Navigate to the directory where you want to create the new app folder.
Run the
generate-corecommand:npx nextjs-electron generate-coreThe CLI will guide you through prompts to configure:
- Target platform(s) for packaging (Windows, macOS, Linux)
- App name
- Output folder path
- Node.js runtime requirement for performance
- GPU acceleration (WebGPU) enablement
- Use of default secure Electron configuration
- Optional additional notes for the README
The generator will scaffold a complete, minimal Electron project in the specified output folder. It then automatically runs
npm installand attempts tonpm startthe new application.Follow the instructions in the generated
README.mdwithin your new app's folder for customization and packaging.
Option 3: Manual Setup (Full Control)
For maximum control over the integration process:
Prepare Your Next.js App: Ensure your
next.config.js(or.ts) hasoutput: 'export'and run:npx next buildThis generates the static
/outdirectory.Create an Electron Entry Point (
electron-main.js): In your project root, createelectron-main.js:// electron-main.js const path = require('path'); async function start() { // Dynamically import launchApp to support both CJS and ESM projects const { launchApp } = await ( eval('typeof module === "undefined" ? import("nextjs-electron") : Promise.resolve(require("nextjs-electron"))') ); // For development, an absolute path to your Next.js 'out' directory is robust. const devAppUrl = `file://\${path.join(__dirname, 'out', 'index.html')}`; // For packaged apps, nextjs-electron's default appUrl is usually sufficient if your 'out' dir is standard. launchApp({ // appUrl: devAppUrl, // Uncomment and adjust if needed for local dev. productName: 'My Awesome Next.js App', // IMPORTANT: Change this! appId: 'com.mycompany.myawesomenextjsapp', // IMPORTANT: Change this! // Add other configurations as needed, see documentation or src/config.ts in the library. }); } start();Important: Customize
productNameandappId.Update
package.json: Set themainfield to your Electron entry file (e.g.,electron-main.js). Add scripts:{ "name": "my-electron-nextjs-app", "version": "1.0.0", "main": "electron-main.js", "scripts": { "start:electron": "electron .", "package:electron": "nextjs-electron package" // Optional: "package:electron:user": "nextjs-electron package --config electron-builder.user.config.js" } // ... your existing dependencies and devDependencies // Ensure nextjs-electron, electron, and electron-builder are listed. }Develop & Test:
npm run start:electronPackage for Distribution: For advanced packaging options (icons, signing, etc.), initialize an
electron-builderconfiguration:npx nextjs-electron init-configThis creates
electron-builder.user.config.js. Review and customize this file. If you rename it toelectron-builder.config.js, thepackage:electronscript will use it. Then, package your app:npm run package:electron # Or target specific platforms: # npx nextjs-electron package --win --mac
CLI Commands
nextjs-electron provides a versatile CLI:
npx nextjs-electron --help: Displays all available commands and options.setup-project: Automatically configures an existing Next.js project fornextjs-electron.- Prompts for
productNameandappId. - Modifies
package.json(adds dependencies, main field, scripts). - Creates a template
electron-main.js.
- Prompts for
generate-core: Generates a new, minimal standalone Electron application for core logic or background tasks.- Interactive prompts for app name, output folder, platforms, Node.js integration, GPU acceleration, etc.
- Creates
package.json,electron-main.js,preload.js,index.html(minimal),.electronrc.json(config template), and a detailedREADME.md. - Automatically runs
npm installandnpm startin the new project.
package: Packages your Electron application usingelectron-builder.--config <path>: Specify a customelectron-builderconfiguration file.--mac,--win,--linux: Target specific platforms.--publish <mode>: Set publish mode (e.g., "onTagOrDraft"). The command will look forelectron-builder.config.jsorelectron-builder.user.config.jsin your project root. If not found, it uses the library's default (not recommended for production).
init-config: Copies the defaultelectron-builder.config.jsfrom the library to your project root aselectron-builder.user.config.js, allowing for easy customization.launch: Launches the Electron app using your project'selectron-main.js(typicallyelectron .). Useful for quick development launches.
Library Usage (launchApp Configuration)
The launchApp function is the core of the library when used manually. It accepts a configuration object to customize various aspects of your Electron application.
// Example: electron-main.js
const { launchApp } = require('nextjs-electron'); // Or import for ESM
launchApp({
productName: 'My Custom App Title',
appId: 'com.custom.app',
windowOptions: {
width: 1200,
height: 800,
// title: 'Overridden by productName, but can be set if productName is not used for window title'
},
appUrl: `file://\${require('path').join(__dirname, 'out', 'index.html')}`, // Adjust path as needed
security: {
csp: "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline';"
},
enableWebGPU: true, // Enable WebGPU related flags
updater: { // Configure electron-updater
autoDownload: true,
autoInstallOnAppQuit: true,
},
logLevel: 'debug', // Set log level for electron-log
// See 'src/config.ts' in the library for all available options and their defaults.
});For detailed configuration options, please refer to the ElectronConfig type and getDefaultConfig function within the library's src/config.ts file.
Development of nextjs-electron (This Library)
To develop this library itself:
- Clone the repository:
git clone https://github.com/app-vyeron-com/nextjs-electron.git cd nextjs-electronnpm installnpm run dev(to build in watch mode with tsup)- To test the library's main process directly with Electron (you might need a sample
outfolder or use thegenerate-corecommand to create one):npm run start:electron(This will runelectron dist/main.jswhich is the library's own main entry for testing)
Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
Ensure your code follows linting and formatting guidelines (npm run lint, npm run typecheck).
