I have a running Electron app and is working great so far. For context, I need to run/open a external file which is a Go-lang binary that will do some background tasks. Basically it will act as a backend and exposing an API that the Electron app will consume.
So far this is what i get into:
I tried to open the file with the "node way" using child_process but i have fail opening the a sample txt file probably due to path issues.
The Electron API expose a open-file event but it lacks of documentation/example and i don't know if it could be useful.
That's it. How i open an external file in Electron ?
There are a couple api's you may want to study up on and see which helps you.
The fs
module allows you to open files for reading and writing directly.
var fs = require('fs'); fs.readFile(p, 'utf8', function (err, data) { if (err) return console.log(err); // data is the contents of the text file we just read });
The path
module allows you to build and parse paths in a platform agnostic way.
var path = require('path'); var p = path.join(__dirname, '..', 'game.config');
The shell
api is an electron only api that you can use to shell execute a file at a given path, which will use the OS default application to open the file.
const {shell} = require('electron'); // Open a local file in the default app shell.openItem('c:\\example.txt'); // Open a URL in the default way shell.openExternal('https://github.com');
Assuming that your golang binary is an executable then you would use child_process.spawn
to call it and communicate with it. This is a node api.
var path = require('path'); var spawn = require('child_process').spawn; var child = spawn(path.join(__dirname, '..', 'mygoap.exe'), ['game.config', '--debug']); // attach events, etc.
If your golang binary isn't an executable then you will need to make a native addon wrapper.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With