Search code examples
node.jselectron

Extrange problem with URL command line parameters on Electron app


I have discovered that if you pass a URL parameter using the command line to an Electron app, it must be the last one. If this condition is not met then electron fails to load the application and returns -1 to the operating system.

Please, consider the follwing main.js electron app:

console.log('Starting..');
const electron = require('electron');
const process = require('process');
const app = electron.app;
console.log(process.argv);
console.log('Quitting..');
app.quit();

and the following exec.cmd file for windows:

call electron main.js %*
ECHO %ERRORLEVEL%

the following execution is successful:

exec param1 param2 param3 https://google.com

It outputs:

Starting..
[
  '<path to electron folder>\\electron.exe',
  'main.js',
  'param1',
  'param2',
  'param3',
  'https://google.com'
]
Quitting..
0

However whatever combination where you add another parameter to the URL parameter fails. For example:

exec https://google.com param2

It outputs only the %ERRORLEVEL% variable value:

-1

Is this a known issue?

How can I prevent it from happening?

I'm on Windows 11, nodejs v20.13.1, electron v11.5.0.

I tried with other electron versions like v31.3.0, but the problem persists.


Solution

  • I don't know why it fails when you pass a URL parameter followed by another parameter, but I found a way to prevent it from failing.

    To make it to work you need to pass -- as the first parameter after main.js. -- means end of command options, after which only positional ("non-option") arguments are accepted.

    For example the following works:

    exec -- https://www.google.com anotherparameter
    

    It outputs:

    Starting..
    [
      '<path to electron folder>\\electron.exe',
      'main.js',
      '--',
      'https://www.google.com',
      'anotherparameter'
    ]
    Quitting..
    0