Electron Packaging and Distribution
After an Electron app is developed, it needs to be packaged into a desktop executable and released to users.
The packaging and distribution process involves tool selection, configuration, signing, update strategies, and more.
Packaging tool introduction
electron-builder
- Highly automated, supports Windows, macOS, Linux
- Supports automatic update functionality
- Simple configuration, suitable for production environments
- Can generate
.exe,.dmg,.AppImageand other formats
electron-packager
- Officially recommended packaging tool, lightweight
- Only generates executables, no automatic updates
- Simple configuration, but needs to be combined with other tools for installers and signing
electron-forge
- Integrates packaging, templates, automatic updates, and release workflows
- Works out of the box, suitable for beginners to quickly start a project
- Built-in Webpack / ESBuild build support
Tool comparison and selection
| Tool | Feature | Advantage | Applicable scenarios |
|---|---|---|---|
| electron-builder | High automation, cross-platform, supports updates | Simple configuration, suitable for production | Complex application release |
| electron-packager | Lightweight, officially supported | Flexible combination with other tools | Simple application packaging |
| electron-forge | Integrated development tools, quick start | Works out of the box, built-in build | Beginners or rapid prototyping |
Configuring packaging parameters
package.json configuration
"build": {
"appId": "com.example.myapp",
"productName": "MyElectronApp",
"directories": {
"output": "dist"
},
"files": ["**/*"],
"extraResources": ["assets/"],
"mac": {
"category": "public.app-category.utilities"
},
"win": {
"target": "nsis",
"icon": "build/icon.ico"
},
"linux": {
"target": ["AppImage", "deb"],
"icon": "build/icon.png"
}
}
Platform-specific configuration
- Windows: installer type (NSIS, Squirrel), icon
.ico - macOS: app category, signing,
.icnsIcon - Linux: target format (AppImage, deb, rpm), icon PNG
Icon and resource files
- Icon size and format must meet platform requirements
- Extra resources can be added via
extraResourcesorextraFilesadd
Multi-platform packaging
Windows packaging
electron-builder --win
- Can generate
.exeor.msiInstallation files - Supports NSIS installer wizard, silent installation
macOS packaging
electron-builder --mac
- Can generate
.dmgor.pkg - With notarization it can pass macOS Gatekeeper
Linux packaging
electron-builder --linux
- Common formats: AppImage, deb, rpm
- Suitable for desktop distribution across different distributions
Cross compilation
- Electron-builder supports cross-platform builds, but macOS apps must be signed on macOS
- Windows / Linux can achieve cross-packaging via Linux or Windows build virtual machines
Code signing
Windows code signing
- Sign with a digital certificate (.pfx)
.exeor.msi - Example:
electron-builder --win --csc-link path/to/certificate.pfx --csc-key-password yourpassword
- Improves user trust, avoids SmartScreen blocking
macOS notarization
- Apple officially requires apps to pass Gatekeeper notarization
- Requires an Apple Developer account
- Build example:
electron-builder --mac --publish never
- Use
notarizeAPI uploads the app for automatic notarization
Certificate application and management
- Windows: purchase an EV or standard code signing certificate
- macOS: apply for a developer certificate with an Apple Developer account
- Certificates must be stored securely to avoid leakage
Automatic updates
Using electron-updater
- Works with electron-builder, supports differential updates
- Installation
electron-updater:npm install electron-updater
Main process example:
const { autoUpdater } = require('electron-updater')
autoUpdater.on('update-available', () => console.log('发现新版本'))
autoUpdater.on('update-downloaded', () => autoUpdater.quitAndInstall())
autoUpdater.checkForUpdates()
Setting up an update server
- Can use GitHub Releases, a private server, or a CDN
- Provide a JSON file describing the latest version information
Differential updates
- Only download changed files, reducing bandwidth consumption
- electron-updater has built-in support for NSIS / Squirrel / AppImage differential updates
Update strategy
- Automatically check for updates (at startup or on a schedule)
- Manually trigger an update
- A popup asks the user whether to update immediately or later
Summary
Core workflow of Electron packaging and distribution:
- Select tools:electron-builder, electron-packager, electron-forge
- Configure parameters: define platform, icon, resources in package.json
- Multi-platform packaging:Windows, macOS, Linux
- Code signing: ensure the app is trusted, avoid security prompts
- Automatic updates: improve user experience, support differential updates
Following best practices lets Electron apps be distributed to users quickly and safely, while making future iteration and updates easier.