Open external file with Electron

There are a couple api’s you may want to study up on and see which helps you.

fs

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
});

path

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');

shell

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');

child_process

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.

addon

If your golang binary isn’t an executable then you will need to make a native addon wrapper.

Leave a Comment