Pass arguments to packaged electron application

后端 未结 2 888
清酒与你
清酒与你 2021-01-18 21:14

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

相关标签:
2条回答
  • 2021-01-18 22:11

    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

    0 讨论(0)
  • 2021-01-18 22:12

    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.

    0 讨论(0)
提交回复
热议问题