Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass arguments to packaged electron application

We're using electron-packager to bundle up and distribute the front-end of our web application. We need to be able to pass in the host and port of the server to the electron front-end for connecting. When we launch via electron main.js --host blah --port 8080 it works. Once it's packaged, we run via ./MyApp --host blah --port 8080 and it doesn't work. This is bad because we don't want customers to need to install electron/npm itself. Also worth noting is that this happens whether we package the app in an asar archive or not.

Any ideas on things we could try, or if we're trying to go about this the wrong way?

like image 497
DTI-Matt Avatar asked Jan 11 '16 21:01

DTI-Matt


People also ask

What is the electron process?

Electron takes a main file defined in your package. json file and executes it. This main file creates application windows which contain rendered web pages and interaction with the native GUI (graphical user interface) of your Operating System. As you start an application using Electron, a main process is created.


1 Answers

Well how are you trying to parse the command line? What does process.argv look like when you start with ./MyApp --host blah --port 8080?

Basically, when you start Electron it looks in its resource folder for 'app', 'app.asar', or 'default_app'; when you start your app with electron main.js --host blah --port what actually happens is that Electron's default app is started which, among other things, parses your command line arguments. When you package your app, it is copied to the the resource folder as 'app' or 'app.asar' and will be started directly when you run MyApp later on. That is to say, you are starting your app in two fundamentally different ways and this is likely the source of your problem.

To mitigate this, what I like to do is to link my development folder into Electron's resource folder during development; this way I can bypass 'default_app' and have the same execution path whether or not the app is packaged.

Having said that, it does not matter which way you start the app, you should definitely be able to parse the command line arguments. For reference, I just set this up in one of my apps with yargs so you should definitely be able to get this to work.

like image 105
inukshuk Avatar answered Sep 20 '22 12:09

inukshuk