Issue 1:
Electron version: 1.4.15

Operating system: Windows 10

I have a simple nodeJS app that has a function to scrape file metadata. Since scraping metadata can be intensive I made the app run this as a child process using fork.

const metaParser = child.fork( fe.join(__dirname, 'parse-metadata.js'), [jsonLoad]);

When run in main.js the process is successfully created, but immediately exits. I added some logging to parse-metadata.js and found out that parse-metadata.js executed successfully and ran long enough to run the first few lines of code and then exited.

How do I get electron to fork parse-metadata.js and keep it alive until the end?

Issue 2:
Electron version: 1.4.14

Operating system: macOS Sierra

Expected behavior

When I fork a process, the process should be able to require native modules compiled for the main electron process and also used by it.

Actual behavior

Because of ELECTRON_RUN_AS_NODE (if that's the name), the forked process thinks it's node and therefore can't find the appropriate native module binary.

Example error:

/private/tmp/kk/node_modules/utp-native/node_modules/node-gyp-build/index.js:28
throw new Error('No native build was found for runtime=' + runtime + ' abi=' + abi + ' platform=' + platform + ' arch=' + arch)
^

Error: No native build was found for runtime=node abi=50 platform=darwin arch=x64
at Function.load.path (/private/tmp/kk/node_modules/utp-native/node_modules/node-gyp-build/index.js:28:9)
at load (/private/tmp/kk/node_modules/utp-native/node_modules/node-gyp-build/index.js:13:23)
at Object.<anonymous> (/private/tmp/kk/node_modules/utp-native/index.js:5:36)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)

How to reproduce

// index.js

const { fork } = require('child_process')

require('utp-native')
console.log('required native dependency from main process')

const ps = fork(`${__dirname}/child.js`)

// child.js

require('utp-native')
console.log('required native dependency from child process')

$ npm install utp-native
$ npm rebuild --runtime=electron --target=1.4.14 --abi=50 --disturl=https://atom.io/download/atom-shell
$ electron index.js

My temporary hack fix

Instead of child_process.fork, use child_process.spawn with process.execPath

Unfortunately, in bundled electron applications, process.execPath isn't electron. Therefore, we need to bundle electron in node_modules/ and use the .extraFiles option in electron-builder to make sure it's available in the bundled app

Now we get the electron path for .spawn using this approach, which works in development and with bundled applications:

let electronPath

try {
// bundled app
electronPath = require(path.join(remoteProcess.resourcesPath, '/../node_modules/electron'))
} catch (_) {
// during developement
electronPath = require(path.join(app.getAppPath(), '/node_modules/electron'))
}

Also this basically doubles our bundle size, as this includes one more (?) copy of electron.

My proposed fix in electron

Allow to unset the runtime override with an option to child_process.fork. I think @sindresorhus has requested this before too.