Open external file with Electron


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 ?


Hey cadizjavier! I have a little bit of experience opening, reading, and writing to files in electron. I used the fs node module. Basically I installed the fs module, and then I used remote.require(‘fs’) to get access to the fs object. If you need some example code or any more details I’d be happy to provide it.

I hope that helps!


Hi. Thanks for reply could you provide me with some examples ? I would like to see how to launch and manage external apps from electron.


I’d be happy to, although launching and managing external applications is different than opening, reading, and writing to files. Which did you mean?


I’m referring to launching and managing external applications. My use case is open an Electron app and launch an external Go app that will expose some API endpoints that will be consumed with the Electron frontend. Ideally the Go process will be launched in background and will be closed when the main Electron app is closed too.


I would really be interested in any examples that are out there for interacting with the filesystem. My particular interest is to load fixed snippets of text data and insert them into the DOM.

@erikmellum I’ll bet others would be interested, too, if you were to post some examples.



My electron app utilizes angular as well. When I first launch the app I load up an FAQ for my application from a file. I create a searchable FAQ using the JSON that I store in a file on the users pc. I also load up settings or create settings depending on whether it is the first time they run it.

Example of reading and parsing a file

 try {
  //test to see if settings exist
  var path = app.getPath('userData') + '/settings.json'
  fs.openSync(path, 'r+'); //throws error if file doesn't exist
  var data=fs.readFileSync(path); //file exists, get the contents
  $rootScope.settings = JSON.parse(data); //turn to js object
} catch (err) {
  //if error, then there was no settings file (first run).
  try {
    var fd = fs.openSync($rootScope.path, 'w+');
    console.log("Created settings file...");
     var newPath = dialog.showOpenDialog({ title: 'Please select a folder',
        defaultPath: app.getPath('userDesktop'), properties: [ 'openDirectory', 'createDirectory']});
  } catch (err) {
    console.log("Error creating settings file: " + JSON.stringify(err));
    throw err;

Example of saving to file:

try {
    fs.writeFileSync(app.getPath('userData') + '/settings.json', angular.toJson($rootScope.settings) , 'utf-8');
    console.log('Saved settings!');
  } catch (err) {
    throw err;

Find out more about reading and writing files in the node fs docs. It is how I learned to do all this.

How to write Code Blocks


You will want to use spawn or exec. My guess is you want to use spawn. Exec will not return until the forked process finishes, plus there is no way to hook into the output streams, it just returns a buffer with the output once it closes. Spawn on the other hand lets you hook into stdout, and stderr so you can see what is happening. To spawn a task I do something like this:

var child = spawn('command', ['args'], {workingDirectory});

Anytime new data is put on stdout or stderr you can hook into these events:

child.stdout.on('data', function (data) });

I usually terminate the process on stderr.
child.stderr.on('data', function (data) {child.kill();});

To detect the process finishing (if it ever does finish)
child.on('close', function (code) { });

But I’m basically just quoting the node ChildProcess docs. Have a look for yourself:

split this topic #9

I moved 2 posts to a new topic: How to write Code Blocks


Thanks everyone!!