How to package an Electron app into a single executable?

yup, it's (now) possible. Choose any of the modules to generate single exe file: electron-builder, electron-forge or windows-installer .


Try using electron-builder -p --win. it will build up the production-ready .exe. I did it with [email protected]

for the publishable build, you'll need the publishing provider in your package.json file, consider the given example.

"build": {
    "appId": "com.trinityinfosystem.electron.exchange.stream",
    "productName": "Accurate",
    "copyright": "Copyright © 2018 Trinity InfoSystem",
    "mac": {
      "category": "public.app-category.utilities",
      "icon": "assets/icon.icns",
      "target": [
        "zip",
        "dmg"
      ],
      "publish": [
        "github"
      ]
    },
    "win": {
      "publisherName": "Trinity InfoSystem",
      "publish": [
        "github"
      ],
      "target": [
        "nsis"
      ]
    },
    "linux": {
      "target": [
        "AppImage",
        "tar.gz"
      ]
    },
    "dmg": {
      "background": "assets/background.png",
      "icon": "assets/icon.icns",
      "title": "Accurate Installer"
    },
    "nsis": {
      "oneClick": false,
      "perMachine": false,
      "allowToChangeInstallationDirectory": true,
      "installerIcon": "assets/icon.ico",
      "installerSidebar": "assets/sidebar.bmp",
      "uninstallerSidebar": "assets/sidebar.bmp",
      "license": "assets/agreement.html",
      "createDesktopShortcut": true,
      "createStartMenuShortcut": true
    },
    "publish": [
      {
        "provider": "github",
        "owner": "vkiranmaniya",
        "repo": "accurate",
        "vPrefixedTagName": true,
        "private": true,
        "releaseType": "draft"
      }
    ]
  },

Add the given pulishing config to your package.json as root proprty. You will need the Github personal access token (here is the Doc) to be exported while running a build.

You can export the token as env variable from main.js as given,

process.env.GH_TOKEN = 'YOUR_PERSONAL_ACCESS_TOKEN_HERE';

If you want to setup AutoUpdate using GitHub, you can use the given module and call checkForUpdates() method from main.js

const electron = require("electron");
const updater = require("electron-updater");
const autoUpdater = updater.autoUpdater;

autoUpdater.on('checking-for-update', function () {
    sendStatusToWindow('Checking for update...');
});

autoUpdater.on('update-available', function (info) {
    sendStatusToWindow('Update available.');
});

autoUpdater.on('update-not-available', function (info) {
    sendStatusToWindow('Update not available.');
});

autoUpdater.on('error', function (err) {
    sendStatusToWindow('Error in auto-updater.');
});

autoUpdater.on('download-progress', function (progressObj) {
    let log_message = "Download speed: " + progressObj.bytesPerSecond;
    log_message = log_message + ' - Downloaded ' + parseInt(progressObj.percent) + '%';
    log_message = log_message + ' (' + progressObj.transferred + "/" + progressObj.total + ')';
    sendStatusToWindow(log_message);
});

autoUpdater.on('update-downloaded', function (info) {
    sendStatusToWindow('Update downloaded; will install in 1 seconds');
});

autoUpdater.on('update-downloaded', function (info) {
    setTimeout(function () {
        autoUpdater.quitAndInstall();
    }, 1000);
});

function checkForUpdates(){
    const data = {
        'provider': 'github',
        'owner':    'vkiranmaniya',
        'repo':     'exchange',
        'token':    'YOUR_PERSONAL_TOKEN_HERE'
      };
    autoUpdater.setFeedURL(data);
    autoUpdater.checkForUpdates();
}

function sendStatusToWindow(message) {
    console.log(message);
}

module.exports = {
    checkForUpdates,
}

Now you can run the command electron-build -p --win to build an auto updatable standalone .exe file. Use --mac or --linux to target specific platform for build.


electron single .exe:

electron-builder --win portable

It generates a single .exe !!!

And you don't need to touch package.json.

More info doc


If electron-builder show some error:

Cannot find module 'fs/promises' Electron JS

Downgrade Electron Builder using: "electron-builder": "22.10.5" or upgrade NodeJS to 14+ More info here.