2

I am trying to create an electron app using .net-core. It is working good in all steps and there are no errors when I test and debug the apps at all. But unfortunately, when I am packaging the app using electron-packager, I get this error when executing the .exe file.

A javaScript error occurred in the main process Uncaught Exception: TypeError [ERR_INVALID_ARG_VALUE]: The argument 'args' in invalid. Received {cwd: 'D:\win\release-builds\gen\resources\app.asar\bin'} at Object.execFile (child_process.js:218:11) at Object.module.(anonymous function) as execFile at exec(child_process.js:160:18) at ELECTRON_ASAR.js:746:23 at portfinder (D:\win\release-builds\gen\resources\app.asar\main.js:122:22) at listen D:.... at Server.server.listen D:.... at Object.onceWrapper (events.js:273:13) at Server.emit (events.js:182:13) at emitListeningNT (net.js:1364:10)

Here is my main.js file's contents:

(function() {
    var childProcess = require("child_process");
    var oldSpawn = childProcess.spawn;
    function mySpawn() {
        console.log('spawn called');
        console.log(arguments);
        var result = oldSpawn.apply(this, arguments);
        return result;
    }
    childProcess.spawn = mySpawn;
})();
const { app } = require('electron');
const { BrowserWindow, dialog, shell } = require('electron');
const fs = require('fs');
const path = require('path');
const process1 = require('child_process').exec;
const portfinder = require('detect-port');
let io, browserWindows, ipc, apiProcess, loadURL;
let appApi, menu, dialogApi, notification, tray, webContents;
let globalShortcut, shellApi, screen, clipboard;
let splashScreen, mainWindowId;

process.env.NODE_ENV = 'development';

const manifestJsonFilePath = path.join(__dirname, 'bin', 'electron.manifest.json');
const manifestJsonFile = require(manifestJsonFilePath);
if (manifestJsonFile.singleInstance) {
    const shouldQuit = app.requestSingleInstanceLock();
    app.on('second-instance', (commandLine, workingDirectory) => {
        mainWindowId && BrowserWindow.fromId(mainWindowId) && BrowserWindow.fromId(mainWindowId).show();
    });

    if (shouldQuit) {
        app.quit();
    }
}

app.on('ready', () => {
    if (isSplashScreenEnabled()) {
        startSplashScreen();
    }

    portfinder(8000, (error, port) => {
        startSocketApiBridge(port);
    });
});

function isSplashScreenEnabled() {
    return Boolean(manifestJsonFile.loadingUrl);
}

function startSplashScreen() {
    let loadingUrl = manifestJsonFile.loadingUrl;
    let icon = manifestJsonFile.icon;

    if (loadingUrl) {
        splashScreen = new BrowserWindow({
            width: manifestJsonFile.width,
            height: manifestJsonFile.height,
            transparent: true,
            frame: false,
            show: false,
            icon: path.join(__dirname, icon)
        });

        if (manifestJsonFile.devTools) {
            splashScreen.webContents.openDevTools();
        }

        splashScreen.loadURL(loadingUrl);
        splashScreen.once('ready-to-show', () => {
            splashScreen.show();
        });

        splashScreen.on('closed', () => {
            splashScreen = null;
        });
    }
}

function startSocketApiBridge(port) {
    io = require('socket.io')(port);
    startAspCoreBackend(port);

    io.on('connection', (socket) => {
        global['electronsocket'] = socket;
        global['electronsocket'].setMaxListeners(0);
        console.log('ASP.NET Core Application connected...', 'global.electronsocket', global['electronsocket'].id, new Date());

        appApi = require('./api/app')(socket, app);
        browserWindows = require('./api/browserWindows')(socket, app);
        ipc = require('./api/ipc')(socket);
        menu = require('./api/menu')(socket);
        dialogApi = require('./api/dialog')(socket);
        notification = require('./api/notification')(socket);
        tray = require('./api/tray')(socket);
        webContents = require('./api/webContents')(socket);
        globalShortcut = require('./api/globalShortcut')(socket);
        shellApi = require('./api/shell')(socket);
        screen = require('./api/screen')(socket);
        clipboard = require('./api/clipboard')(socket);

        if (splashScreen && !splashScreen.isDestroyed()) {
            splashScreen.close();
        }
    });
}

function startAspCoreBackend(electronPort) {
    portfinder(8000, (error, electronWebPort) => {
        loadURL = `http://localhost:${electronWebPort}`
        const parameters = [`/electronPort=${electronPort}`, `/electronWebPort=${electronWebPort}`];
        let binaryFile = manifestJsonFile.executable;

        const os = require('os');
        if (os.platform() === 'win32') {
            binaryFile = binaryFile + '.exe';
        }

        const binFilePath = path.join(__dirname, 'bin', binaryFile);
        var options = { cwd: path.join(__dirname, 'bin') };
        apiProcess = process1(binFilePath, parameters, options);

        apiProcess.stdout.on('data', (data) => {
            console.log(`stdout: ${data.toString()}`);
        });
    });
}

the package.json file's contents:

{
  "name": "XXXXXXXX",
  "version": "1.0.0",
  "description": "XXXXXXXXXXXXXXXXXXX",
  "main": "main.js",
  "author": "Gregor Biswanger",
  "license": "MIT",
  "scripts": {
    "start": "electron .",
    "package-mac": "electron-packager . --overwrite --platform=darwin --arch=x64 --icon=assets/icons/mac/icon.icns --prune=true --out=release-builds",
    "package-win": "electron-packager . electron-tutorial-app --overwrite --asar=true --platform=win32 --arch=ia32 --icon=assets/icons/win/icon.ico --prune=true --out=release-builds --version-string.CompanyName=CE --version-string.FileDescription=CE --version-string.ProductName=\"Electron Tutorial App\"",
    "package-linux": "electron-packager . electron-tutorial-app --overwrite --asar=true --platform=linux --arch=x64 --icon=assets/icons/png/1024x1024.png --prune=true --out=release-builds"
  },
  "dependencies": {
    "detect-port": "^1.2.3",
    "socket.io": "^2.1.1"
  },
  "devDependencies": {
    "@types/node": "^10.11.0",
    "@types/socket.io": "^1.4.38",
    "electron": "^3.1.13",
    "electron-packager": "^14.0.4",
    "tslint": "^5.11.0",
    "typescript": "^3.0.3"
  },
  "keywords": [
    "genetic"
  ]
}

and the program.cs file:

public static void Main(string[] args)
{
    BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
    return WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseElectron(args)
        .Build();
}
H. beyraghdar
  • 83
  • 1
  • 7
  • It would be best if you could put the error within the question as text, helps other searching on a similar issue – Ralph Willgoss Aug 25 '19 at 20:38
  • 1
    Please don't use DropBox or any other third-party image service, @H.beyraghdar. Use the `Add image` function of the question editor to upload the picture to StackExchange's Imgur service. Better than that, inline the error message using a blockquote (`> `). See also: [ask]. Thanks! – Alexander Leithner Aug 26 '19 at 06:58

0 Answers0