I'm building a command line interface in node which connects to a background command line daemon. If no daemon is running, the first time the cli is called it will fork off the daemon using child_process.fork
The daemon needs to launch instances of electron BrowserWindow, but requiring electron is showing unusual behavior.
If running the daemon on it's own in the foreground, everything works smoothly; however in the background I get an empty module when requiring electron.
Printing Object.keys(require('electron'))
to console shows the number sequence 0..84
, and printing the results of require('electron')
shows the string /path/to/electron/dist/electron
Printing out process.argv
shows that the forked script is definitely being executed with electron.
I'm stumped. Any direction would be greatly appreciated.
Example:
launcher
#!/usr/local/bin/electron
const cp = require('child_process');
console.log();
const cld = cp.fork(__dirname+'/daemon',{
stdio:['inherit','inherit','inherit','ipc']
});
cld.on('message', (code) => {
code = parseInt(code);
cld.disconnect();
process.exit(code);
});
daemon
#!/usr/local/bin/electron
const fs=require('fs');
const log = (x)=>fs.appendFileSync('log',x+'\n\n');
log('');
if(!process.send) process.send = console.log;
log(process.argv);
const e = require('electron');
log(e);
log(Object.keys(e));
log(e.app);
process.send(0);
Resulting log file
*removed*/lib/thirdparty/node_modules/electron/dist/electron,*removed*/tmp/daemon
*removed*/lib/thirdparty/node_modules/electron/dist/electron
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84
undefined
Log file from running just daemon
*removed*/lib/thirdparty/node_modules/electron/dist/electron,./daemon
[object Object]
clipboard,nativeImage,shell,app,autoUpdater,BrowserView,BrowserWindow,contentTracing,crashReporter,dialog,globalShortcut,ipcMain,inAppPurchase,Menu,MenuItem,net,netLog,Notification,powerMonitor,powerSaveBlocker,protocol,screen,session,systemPreferences,TopLevelWindow,TouchBar,Tray,View,webContents,WebContentsView
[object App]
forked process by default set ELECTRON_RUN_AS_NODE=1
and will not expose any electron specific modules:
as https://github.com/electron/electron/issues/6656 says, you may need workaround by explicitly invoke process separately vice versa.