Due to the nature of Electron, building Electron applications as Flatpaks requires a few extra steps compared with other applications. Thankfully, several tools and resources are available which make this much easier.
This guide provides information on how building Electron applications differs from other applications. It also includes information on the tooling for building Electron applications and how to use it.
Building the sample application#
While it isn’t strictly necessary, you might want to try building and running the sample application yourself.
To get setup for the build, download or clone the sample app from GitHub,
and navigate to the
/flatpak directory in the terminal. You must also
install the Electron base app and the Node.js SDK extension:
$ flatpak install flathub org.electronjs.Electron2.BaseApp//23.08
$ flatpak install flathub org.freedesktop.Sdk.Extension.node18//23.08
Then you can run the build:
$ flatpak-builder build org.flathub.electron-sample-app.yml --install --force-clean --user
Finally, the application can be run with:
$ flatpak run org.flathub.electron-sample-app
The first part of the sample application’s manifest specifies the application’s ID. It also configures the runtime and SDK:
The Freedesktop runtime is generally the best runtime to use with Electron applications, since it is the most minimal runtime, and other dependencies will be specific to Electron itself.
The Electron base app#
Next, the manifest specifies that the Electron base app should be used, by
base-version properties in the application
Base apps are described in Dependencies. Using the Electron base app is much faster and more convenient than manually building Electron dependencies. It also has the advantage of reducing the amount of duplication on users’ machines, since it means that Electron is only saved once on disk.
The Node.js SDK extension#
In order to build Electron-based apps, you need Node.js available at build time. Flathub provides Node.js LTS versions as extensions for the SDK, so you can install one of them and add it in your apps’ manifest:
Enable the extension by adding it to
Note that the extension name (last portion of reverse-dns notation,
in this example) must be the same in
command property indicates that a script called
run.sh is to be
executed to run the application. This will be explained in further detail
The standard guidelines on sandbox permissions apply to Electron applications. However, Electron does not use Wayland by default. So for display access, only X11 should be used as the default configuration. This will make Electron use Xwayland in a wayland session and nothing else is required.
The sample app also configures pulseaudio for sound and enables network access.
Native wayland support in electron is experimental and often unstable. It is advised to stick with the X11 and Xwayland configuration above as the default.
To enable experimental native Wayland support in Electron>=20, the
--ozone-platform-hint=auto flag can be passed to the program. auto
will choose Wayland when the seesion is wayland and Xwayland or X11
The recommended option is to leave it to the user. So
should be used in manifest and Wayland can be tested with:
flatpak run --socket=wayland org.flathub.electron-sample-app
To make native wayland the default for users
--socket=wayland must be used in the manifest.
Client-side window decorations in native wayland can be enabled by
libnotify on Linux to provide desktop notifications.
libnotify since 0.8.0
automatically uses the notification portal
when inside a sandboxed environment and
is not required.
branch/23.08 comes with
These build options aren’t strictly necessary, but can be useful if something
env allows setting an array of environment variables, in this case we set
info so that
npm gives us more detailed
cflags: -O2 -g
cxxflags: -O2 -g
The application module#
The final section of the manifest defines how the application module should be built. This is where some of the additional logic for Electron and Node.js can be found.
flatpak-builder doesn’t allow build tools to access the
network. This means that tools which rely on downloading sources will not
work. Therefore, Node.js packages must be downloaded prior to running the
build. Setting the
electron_config_cache environment variable means
that these will be found when it comes to the build.
The next part of the manifest describes how the application should be built. The simple buildsystem option is used, which allows a sequence of commands to be specified, which are used for the build. The download location and hash of the application are also specified.
- type: archive
Bundling NPM packages#
The next line is how NPM modules get bundled as part of Flatpaks:
Since even simple Node.js applications depend on dozens of packages, it would be impractical to specify all of them as part of a manifest file. A Python script has therefore been developed to download Node.js packages with NPM or Yarn and include them in an application’s sources.
The Python script requires a
yarn.lock) file. This
file contains information about the packages that an application depends on, and
can be generated by running
npm install --package-lock-only from an
application’s root directory. The script is then run as follows:
$ flatpak-node-generator npm package-lock.json
This generates the manifest JSON needed to build the NPM/Yarn
packages for the application, which are outputted to a file called
generated-sources.json. The content of this file can be copied to
the application’s manifest but, because it is often very long, it is
often best to link to it from the main manifest, which is done by adding
generated-source.json as a line in the manifest section, as seen above.
Launching the app#
The Electron app is run through a simple script. This can be given any name
but must be specified in the manifest’s
"command": property. See below
a sample wrapper for launching app:
- type: script
- zypak-wrapper.sh /app/main/electron-sample-app "$@"
Last but not least, since the simple build option is being used, a list of
build commands must be provided. As can be seen,
npm is run with the
npm_config_offline=true environment variable, installing dependencies from
packages that have already been cached. These are copied to
run.sh script is installed to
/app/bin/ so that it will be
# Install npm dependencies
- npm install --offline
# Build the app; in this example the `dist` script
# in package.json runs electron-builder
npm run dist -- $ELECTRON_BUILDER_ARCH_ARGS --linux --dir
# Bundle app and dependencies
- cp -a dist/linux*unpacked /app/main
# Install app wrapper
- install -Dm755 -t /app/bin/ ../run.sh
Note that if the application you are trying to package contains a
build block in
package.json with instructions for Linux, this can cause
electron-builder to try to fetch additional binaries at build-time (Even if –dir option is used). The following example shows a configuration that will try to download AppImage binaries:
The preferred way of fixing this, is not a patch, but a build-time edit using
jq. The following command will replace
"target": "AppImage" with
jq '.build.linux.target="dir"' <<<$(<package.json) > package.json