Skip to content

Instantly share code, notes, and snippets.

@ef2k
Last active May 28, 2018 16:47
Show Gist options
  • Save ef2k/b33862c53ca6e4336f504a296a32f7c6 to your computer and use it in GitHub Desktop.
Save ef2k/b33862c53ca6e4336f504a296a32f7c6 to your computer and use it in GitHub Desktop.
Unpackaging files in Electron

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)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment