Electron allows execution via child_process.execFile
but, restricts fs
, fork
and exec
in the production runtime unless the executable is unpacked.
Asar is the tar-like archiving format used by Electron. All packed files are treated as "read only". To execute a distributed file, electron-builder
needs to be configured to leave our executable as an "unpacked file".
In package.json,
"build": {
"asarUnpack": [
"lib/mybin"
]
}
In the main process,
import path from 'path'
import { exec } from 'child_process'
//...
const execPath = path.join(process.cwd(), 'lib', 'mybin')
const child = exec(execPath, { env }, (err, stdout, stderr) => {
if (error) {
// log the fatal error
return
}
console.log(stdout)
console.error(stderr)
})