A complete solution to package and build a ready for distribution Electron app for MacOS, Windows and Linux with “auto update” support out of the box.
- NPM packages management:
- Native application dependencies compilation (only if the two-package.json project structure is used).
- Development dependencies are never included. You don't need to ignore them explicitly.
- Code Signing on a CI server or development machine.
- Auto Update ready application packaging.
- Build version management.
- Numerous target formats:
- Publishing artifacts to GitHub Releases and Bintray.
Note: Platform specific 7zip-bin-*
packages are optionalDependencies
, which may require manual install if you have npm configured to not install optional deps by default.
Real project example — onshape-desktop-shell.
Consider to use nsis
target for Windows (auto-update will be implemented this month) for new projects.
We recommend to use two package.json files (it is not required, you can build your project with any structure).
-
For development (
./package.json
)The
package.json
resides in the root of your project. Here you declare the dependencies for your development environment and build scripts (devDependencies
). -
For your application (
./app/package.json
)The
package.json
resides in theapp
directory. Declare your application dependencies (depencencies
) here. Only this directory is distributed with the final, packaged application.
Why?
- Native npm modules (those written in C, not JavaScript) need to be compiled and here we have two different compilation targets for them. Those used within the application need to be compiled against the electron runtime and all
devDependencies
need to be compiled against your local node.js environment. Thanks to the twopackage.json
structure, this is trivial (see #39). - No need to specify which files to include in the app (because development files reside outside the
app
directory).
Please see Loading App Dependencies Manually and #379.
See options for a full reference but consider following the simple guide outlined below first.
For an app that will be shipped to production, you should sign your application. See Where to buy code signing certificates.
-
Specify the standard fields in the application
package.json
— name,description
,version
and author. -
Specify the build configuration in the development
package.json
as follows:"build": { "appId": "your.id", "mac": { "category": "your.app.category.type", }, "win": { "iconUrl": "(windows-only) https link to icon" } }
See all options.
-
Create a directory build in the root of the project and save a
background.png
(macOS DMG background),icon.icns
(macOS app icon) andicon.ico
(Windows app icon) into it.The Linux icon set will be generated automatically based on the macOS
icns
file (or you can put them into thebuild/icons
directory if you want to specify them yourself. The filename must contain the size (e.g.32x32.png
) of the icon). -
Add the scripts key to the development
package.json
:"scripts": { "pack": "build --dir", "dist": "build" }
Then you can run
npm run dist
(to package in a distributable format (e.g. dmg, windows installer, deb package)) ornpm run pack
(only generates the package directory without really packaging it. This is useful for testing purposes).If you use the two-package.json project structure, you'll only have your
devDependencies
in your developmentpackage.json
and yourdependencies
in your apppackage.json
. To ensure your dependencies are always updated based on both files, simply add"postinstall": "install-app-deps"
to your developmentpackage.json
. This will basically automatically trigger annpm install
within your app directory so you don't have to do this work everytime you install/update your dependencies. -
If you have native addons of your own that are part of the application (not as a dependency), add
"nodeGypRebuild": true
to thebuild
section of your developmentpackage.json
.
💡 Don't use npm (neither.npmrc
) for configuring electron headers. Use node-gyp-rebuild bin instead. -
Installing the required system packages.
Please note that everything is packaged into an asar archive by default.
electron-builder
produces all required artifacts:
.dmg
: macOS installer, required for the initial installation process on macOS.-mac.zip
: required for Squirrel.Mac..exe
and-ia32.exe
: Windows installer, required for the initial installation process on Windows. Please note that your app must handle Squirrel.Windows events. See real world example..full-nupkg
: required for Squirrel.Windows.
To benefit from auto updates, you have to implement and configure Electron's autoUpdater
module (example).
You also need to deploy your releases to a server.
Consider using Nuts (uses GitHub as a backend to store the assets), Electron Release Server or Squirrel Updates Server.
See the Publishing Artifacts section of the Wiki for more information on how to configure your CI environment for automated deployments.
For Windows consider only distributing 64-bit versions. Or use NSIS.
Execute node_modules/.bin/build --help
to get the actual CLI usage guide.
Building:
--mac, -m, -o, --osx, --macos Build for MacOS, accepts target list (see
https://goo.gl/HAnnq8). [array]
--linux, -l Build for Linux, accepts target list (see
https://goo.gl/O80IL2) [array]
--win, -w, --windows Build for Windows, accepts target list (see
https://goo.gl/dL4i8i) [array]
--x64 Build for x64 [boolean]
--ia32 Build for ia32 [boolean]
--dir Build unpacked dir. Useful to test. [boolean]
--extraMetadata, --em Inject properties to application package.json
Publishing:
--publish, -p Publish artifacts (to GitHub Releases), see
https://goo.gl/WMlr4n
[choices: "onTag", "onTagOrDraft", "always", "never"]
--draft Create a draft (unpublished) release [boolean]
--prerelease Identify the release as a prerelease [boolean]
Deprecated:
--platform The target platform (preferred to use --mac, --win or --linux)
[choices: "mac", "osx", "win", "linux", "darwin", "win32", "all"]
--arch The target arch (preferred to use --x64 or --ia32)
[choices: "ia32", "x64", "all"]
Other:
--help Show help [boolean]
--version Show version number [boolean]
Examples:
build -mwl build for MacOS, Windows and Linux
build --linux deb tar.xz build deb and tar.xz for Linux
build --win --ia32 build for Windows ia32
build --em.foo=bar set application package.json property `foo` to `bar`
See node_modules/electron-builder/out/electron-builder.d.ts
. Typings is supported.
"use strict"
const builder = require("electron-builder")
const Platform = builder.Platform
// Promise is returned
builder.build({
targets: Platform.MAC.createTarget(),
devMetadata: {
"//": "build and other properties, see https://goo.gl/5jVxoO"
}
})
.then(() => {
// handle result
})
.catch((error) => {
// handle error
})
See the Wiki for more documentation.