问题
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?
回答1:
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.
回答2:
I recommend you to use a command line arguments management system like "minimist" for example.
You can use this in your json : "start": "electron . --srv=server.com --prt=112 --arg3=myarg3"
In your main.js you can use this :
var args = require('minimist')(process.argv);
console.log(args)
and you can use your args in the main javascript file.
For the Package, you can do the same thing but in the shortcut, by adding myapp.exe --srv=server.com --prt=112 --arg3=myarg3
来源:https://stackoverflow.com/questions/34731875/pass-arguments-to-packaged-electron-application