Run main process as child_process


#1

Hi everybody!
This shouldn’t be such a problem, however I have had troubles doing this. First of all, I am using the pre-compiled binary app that you can download from Electron’s website:

  • I am using the usual pre-compiled binary on Mac OS X.
  • In myapp.app/Contents/Resources I made a folder app as described.
  • I added a brief package.json inside it, setting index.js as main script.

Now, if I add to index.js the following snippet:

'use strict';

var electron = require('electron');
var app = electron.app;

const BrowserWindow = electron.BrowserWindow;

var mainWindow;

function createWindow () {
  mainWindow = new BrowserWindow({width: 800, height: 600});
  mainWindow.loadURL('file://' + __dirname + '/index.html');
  mainWindow.webContents.openDevTools();
  mainWindow.on('closed', function()
  {
    mainWindow = null;
  });
}

app.on('ready', createWindow);

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin')
  {
    app.quit();
  }
});

app.on('activate', function () {
  if (mainWindow === null)
  {
    createWindow();
  }
});

(that is basically the example code to get things working) everything works fine. I get the window and I can get it to do basically anything.

Now, for reasons related to updates, I am in need to slightly change this paradigm. What I would need is to be able to perform several tasks from index.js without any need to do any gui operation (it should be some sort of daemon) and then to start some child.js script as a child_process from index.js. child.js should be able to open windows and all the rest.

So here was my naive try. I just cut and pasted the example snippet above in child.js, then edited index.js into the following:

var child_process = require('child_process');
var my_child = child_process.fork(__dirname + '/child.js');

Quite minimal, right? Hoped it would work, but it didn’t. When I double click on my pretty app, nothing happens. I bet I am doing something wrong in a trivial way, but I wouldn’t be able to tell what.

Can anybody help?
Thank you so much


#2

Have you checked out the electron-updater module?


#3

Sure I did! It’s quite limited and not portable though. I have spent quite a lot of time figuring out the way of deploying updates myself that would best suit my needs, and now I am one child_process away from getting what I need! I thought it would be trivial; is it not?


#4

Update 1: I moved this out of my package so that I could get console.logs. child.js dies with an error at require('electron'): it doesn’t seem to be able to find it.


#5

Update 2: I listed the environment variables in child.js and noticed a ATOM_SHELL_INTERNAL_RUN_AS_NODE = 1. I thought I should turn that to 0, but nothing changed.


#6

For future reference this has also been cross-posted to StackOverflow.