Packaging and Distribution
8 min read
Welcome to the final boss of desktop application development! You have spent hours writing your HTML, CSS, JavaScript, and interacting with native operating system APIs. Your application works beautifully on your local machine.
But how do you give it to your users?
Definition: Packaging and distribution is the process of bundling your application's source code, assets, and the Electron framework into a single, platform-specific executable (like an .exe or .app), and then wrapping it in a user-friendly installer.
The Problem: You cannot just send your user a ZIP file containing a package.json, an index.js, and a node_modules folder, then expect them to open their terminal, install Node.js, and run npm start. Users expect a standard, double-clickable installer that puts a shiny icon on their desktop.
The Solution: Build tools automate the process of compiling your application, wrapping it in the Electron binary, and generating an installer so that your users can install your application with zero technical knowledge.
Imagine you run a bakery. You do not hand a customer a bag of raw flour, two eggs, and a whisk. You bake the cake (compile), put it in a nice cardboard box (installer), and put a tamper-proof sticker on the lid (code signing). Let's learn how to run our digital bakery! π
1. Choosing Your Tool: Electron Forge vs. Electron Builder π οΈ
Definition: Build tools are command-line programs that manage the heavy lifting of turning your raw source code into distributable desktop applications for different operating systems.
The Problem: Compiling an Electron app manually means downloading the pre-built Electron binary for Windows, macOS, and Linux, replacing the default app icon, securely packing your source code, and manually generating installers. Doing this by hand is a tedious, error-prone nightmare.
The Solution: The industry has two major players that handle this entire process for you: Electron Forge and Electron Builder.
Electron Forge (The Official Toolkit)
Electron Forge is maintained by the core Electron team. It is a modular toolkit that glues together single-purpose packages (like @electron/packager and @electron/rebuild) into one unified pipeline.
- Best for: Starting fresh. You can scaffold a brand-new application complete with a React/Vue/Webpack setup in one command.
- Architecture: Highly modular. It uses "Makers" to build installers and "Publishers" to upload them to GitHub or AWS.
- Getting Started:bash
npx create-electron-app@latest my-awesome-app --template=webpack
Electron Builder (The Community Heavyweight)
Electron Builder is a massively popular, third-party solution. It is known as the "batteries-included" tool because it handles everything internally via a single configuration block in your package.json.
- Best for: Existing projects and advanced workflows. It has built-in, out-of-the-box support for auto-updates, multi-platform CI/CD servers, and Docker-based cross-compilation.
- Architecture: Configuration-based. You tell it what you want in your
package.json, and it figures out the rest. - Getting Started:bash
npm install electron-builder --save-dev
Industry Standard Tip: While Electron Forge is the official recommendation and fantastic for new projects, Electron Builder is heavily used in the enterprise world because of its incredibly seamless integration with electron-updater (which handles automatic background updates). For this guide, we will look at workflows that apply to both.
2. Compiling Assets and Creating Standalone Executables ποΈ
Definition: Compiling and packaging is the act of combining your HTML/JS/CSS assets with the Electron framework and rebuilding any native Node.js modules so they run on the user's specific operating system.
The Problem: JavaScript is generally cross-platform, but Node.js is not always. If you use a database like SQLite, it relies on C++ code bindings. A C++ module compiled on your Mac will completely crash if a Windows user tries to run it. Furthermore, leaving your source code as plain .js files exposes your proprietary logic to anyone who downloads the app.
The Solution: The build tool will first rebuild native dependencies for the target operating system (e.g., creating .node files specific to Windows). Then, it packs all your application code into an app.asar file.
What is an ASAR file?
ASAR (Atom Shell Archive Format) is a simple extensive archive format. Electron reads files directly from this archive without unpacking it.
- Performance: It bypasses OS file limits and speeds up
require()calls because the OS doesn't have to scan thousands of small files innode_modules. - Privacy: It slightly obfuscates your code. (Note: It is not encrypted! Dedicated users can still unpack an ASAR file, but it stops casual snooping).
Hands-on: Building the Executable
In a standard Electron Builder setup, you configure your package.json to tell the builder what to compile:
{
"name": "my-awesome-app",
"version": "1.0.0",
"build": {
"appId": "com.mycompany.myawesomeapp",
"asar": true,
"directories": {
"output": "dist"
}
},
"scripts": {
"dist": "electron-builder"
}
}Running npm run dist will package the app, bundle it inside the Electron framework, and output a raw executable (like MyAwesomeApp.app for macOS or MyAwesomeApp.exe for Windows).
3. The Basics of Code Signing for macOS and Windows π
Definition: Code signing is a security technology that uses cryptographic hashes and digital certificates to prove that an application was created by a specific developer and has not been maliciously altered since it was signed.
The Problem: Operating systems inherently distrust executable files downloaded from the internet. If you distribute your packaged app without signing it:
- macOS will show a scary "App is damaged and cannot be opened" Gatekeeper error.
- Windows will block the application with a giant blue Microsoft Defender SmartScreen warning ("Windows protected your PC").
Users will panic, think your app is a virus, and delete it.
The Solution: You must cryptographically sign your application using a verified certificate from a trusted Certificate Authority (CA) or Apple.
macOS: Developer ID and Notarization π
To sign an app for macOS, you need a Mac machine, Xcode, and an Apple Developer account ($99/year).
- Code Signing: You sign the application with your "Developer ID Application" certificate.
- Notarization: This is mandatory. After signing, your build tool automatically uploads your app to Apple's servers. Apple scans it for malware. If it passes, Apple issues a "ticket" that gets stapled to your app. Now, macOS Gatekeeper will let it run without warnings.
Windows: The EV Certificate Rule πͺ
To sign an app for Windows, you need to purchase a Code Signing Certificate from a vendor (like DigiCert or Sectigo).
π¨ Crucial Industry Update (Since June 2023): Microsoft now strictly requires an EV (Extended Validation) Code Signing Certificate to bypass SmartScreen warnings immediately.
- Standard (OV) certificates no longer build reputation easily.
- EV Certificates are physically bound to a highly secure USB hardware dongle or a Cloud HSM (Hardware Security Module). You cannot just download the certificate file. Your build server must have physical access to the USB stick, or API access to a Cloud HSM, to sign the Windows executable.
How it works under the hood:
When your build tool signs the app, it runs the code through a hashing algorithm (like SHA-256) to create a unique fingerprint. It then encrypts this fingerprint with your private key. When the user opens the app, the OS decrypts the fingerprint using your public key and re-hashes the app. If the fingerprints match, the OS knows the file wasn't tampered with by a hacker during download.
4. Generating Installers (DMG, Setup.exe, AppImage) πΏ
Definition: An installer is a specialized program or disk image that safely extracts your compiled application into the correct system folders, creates shortcuts, and registers uninstallation protocols.
The Problem: A raw executable is messy. If you just give a user app.exe, they will likely leave it in their Downloads folder. They won't have a Start Menu shortcut, and there will be no clean way to uninstall the app later.
The Solution: We package the executable into platform-standard installation formats.
The Big Three Formats:
1. Windows: NSIS (Setup.exe)
NSIS (Nullsoft Scriptable Install System) is the industry standard for Windows Electron apps.
- What it does: It creates the classic
Setup.exewizard. - Why we use it: It supports "one-click" installations, automatically places the app in
AppDataorProgram Files, adds a shortcut to the Desktop and Start Menu, and cleanly handles automatic updates in the background.
2. macOS: DMG (Disk Image)
Mac users do not typically use setup wizards. They expect a .dmg file.
- What it does: It opens a virtual window showing your app icon and an arrow pointing to the
Applicationsfolder shortcut. - Why we use it: It is the culturally accepted way to install software on macOS. The user drags the app into the folder, and macOS handles the rest.
3. Linux: AppImage or Snap
Linux distribution is fragmented (Ubuntu, Fedora, Arch, etc.), so creating standard installers is tricky.
- What it does: AppImage is a single file that contains everything the app needs to run. No installation required; the user just marks it as executable and runs it.
- Why we use it: It works across almost all Linux distributions flawlessly without needing to mess with
aptorpacmanpackage managers.
Hands-on: Configuring Installers in Electron Builder
With electron-builder, generating these installers is as simple as defining your targets in your package.json. The builder will automatically download the required tools (like the NSIS compiler) and generate them!
{
"build": {
"appId": "com.mybakery.app",
"mac": {
"target": "dmg",
"icon": "build/icon.icns"
},
"win": {
"target": "nsis",
"icon": "build/icon.ico"
},
"linux": {
"target": "AppImage"
},
"nsis": {
"oneClick": true,
"perMachine": false,
"createDesktopShortcut": true
}
}
}When you run your build command, Electron Builder will output three pristine files:
MyBakeryApp-1.0.0-mac.dmgMyBakeryApp Setup 1.0.0.exeMyBakeryApp-1.0.0.AppImage
You are now ready to upload these to your website, GitHub Releases, or AWS S3, and ship your desktop application to the world! π
References & Extra Reading π
Challenge
Challenge: Configure electron-builder for Cross-Platform Distribution
You are tasked with preparing an Electron application for production. Your goal is to fill out the build configuration object within the package.json file for electron-builder to handle packaging, code signing, and installer creation for macOS, Windows, and Linux.
Requirements:
- Application Identity: Set the unique application ID to
com.prodev.dataviz-tool. - Packaging: Configure the application to be packed into an
app.asararchive. - macOS Configuration (
mac):- Set the application category to
public.app-category.developer-tools. - Specify the target installer format as
dmg. - Enable
hardenedRuntimefor security. - Provide the signing identity placeholder:
Developer ID Application: ProDev Inc. (XYZ123ABCDE). - Add the Apple Team ID placeholder for notarization:
ZYX987WVUT.
- Set the application category to
- Windows Configuration (
win):- The target installer must be
nsis. - To satisfy new requirements, specify the
certificateSubjectNamefor an EV certificate using the placeholder:CN=ProDev Inc, O=ProDev Inc, C=US.
- The target installer must be
- Linux Configuration (
linux):- The target distribution format should be
AppImage.
- The target distribution format should be
Expected result
Your final code should be a valid package.json file where the build object is fully populated with keys and values that satisfy all the requirements for appId, asar, mac, win, and linux configurations, including targets, categories, and code signing placeholders.