Pass arguments to packaged electron application

南笙酒味 提交于 2019-12-01 19:57:49

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!