How to get the original path of a portable Electron app?

From the main process:

// If not already defined...
const { app } = require ('electron');
const path = require ('path');

let execPath;

execPath = path.dirname (app.getPath ('exe'));
// or
execPath = path.dirname (process.execPath);

From a renderer process:

// If not already defined...
const { remote } = require ('electron');
const path = require ('path');

let execPath;

execPath = path.dirname (remote.app.getPath ('exe'));
// or
execPath = path.dirname (remote.process.execPath);

I found a solution: Use the Environment Variable (created by Electron-Builder)

process.env.PORTABLE_EXECUTABLE_DIR

to show the real Path of the App.exe. Works only packed with Electron-Builder


I had a lot of trouble with this and was finally able to solve the issue by replacing __dirname by '.', see working example below :

const path = require('path')
const myAppPath = path.resolve('.', 'myapp.exe');