Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to package external .exe files in electron application

I am developing an electron application where I am using external .exe files. In development mode application works fine. But when I package the application, it is unable to find those .exe files.

How to include .exe files in packaged application. I am using electron-builder for packaging.

Below is my configuration,

"build": {
    "appId": "com.f.quickstart",
    "files": [
      "app/build/**/*",
      "app/build/**/*.exe",
      "main.js",
      "mainIPC.js",
      "src/**/*",
      "dist/**/*",
      "node_modules/**/*",
      "package.json"
    ],
    "win": {
      "target": "portable",
      "signAndEditExecutable": false
    },
    "linux": {
      "target": "AppImage"
    },
    "mac": {
      "target": "zip"
    }
  }
like image 409
Mangesh Tak Avatar asked Oct 24 '25 12:10

Mangesh Tak


1 Answers

Note that I'm using: electron ^12.0.0, electron-builder ^23.6.0, and Windows 11.

During Installation

If you only need the exe in your installer, you can just use a resources directory. This is useful, for example, when you want to run an exe that installs dependencies during your app's installation.

Here's an example using an nsis installer:

  1. Create a resources directory in your root directory.
  2. Add everything you'll need in the build rule:
// package.json

{
...

  "build": {
    "appId": "mycompany.MyProductName",
    "productName": "MyProductName",
    "directories": {
      "buildResources": "resources"
    },
    "nsis": {
      "oneClick": false,
      "perMachine": true
    },
    "win": {
      "target": "nsis"
    },
    "extends": null
  }
}
  1. Create a vendor directory within your new resources directory.

  2. Drag your exe into the vendor directory.

  3. Create a file called installer.nsh within the resources directory, and write something like:

!define vendordir "${BUILD_RESOURCES_DIR}\vendor"

!define myexe_64 "myexe.exe"
!define myexe "myexe_x86.exe"

!macro customInstall
  ${If} ${RunningX64}
    ExecWait '"${vendordir}\${myexe_64}"'
  ${Else}
    ExecWait '"${vendordir}\${myexe}"'
  ${EndIf}
!macroend
  1. Run the electron builder dist command for Windows.

During Runtime

Using an exe during runtime is such a common use case that I'm honestly surprised how little documentation I could find for this.

Here's an example of how I do it:

  1. Create a directory called extras anywhere in your project. I put mine at src/main/extras.

  2. Add an entry for extraFiles in your build rule:

// package.json

{
...

  "build": {
    "extraFiles": [
      {
        "from": "src/main/extras",
        "to": "extras",
        "filter": [
          "**/*"
        ]
      }
    ],
  }
}
  1. You can now refer to the exe by using the path to your app, but you need to change the path based on development and production. For this, we can use electron-is-dev:
// preload.js

const { remote } = require('electron');
const path = require('path');
const isDev = require('electron-is-dev');

const appPath = remote.app.getAppPath();
const extrasPath = isDev ?
    path.join(appPath, 'src', 'main', 'extras') :
    path.join(appPath, '..', '..', 'extras');

// We finally have our exe!
const exePath = path.join(extrasPath, 'myexe.exe');

The Electron Builder configuration docs are still a good resource for details.

Hope this helps!

like image 138
Chris Pavs Avatar answered Oct 27 '25 02:10

Chris Pavs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!