Search code examples
javascriptnode.jschild-processspawnelectron

Pass a packed JS file within an asar package to a spawned node child process


When trying to spawn a node.js child_process from within my (previously atom-shell) that runs node path/to/a/packed/file I am getting Cannot find module error because the path is within the app.asar archive (where asar is the tar-like archive format).

Error: Cannot find module 'C:\Program Files (x86)\my-app\resources\app.asar\bootstrap\daemon'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

I understand this is because the unpatched node is trying to locate the bootstrap/daemon module within the archive, as though it is a physical file.

Can someone who is more familiar with the asar documentation/code advise a workaround for this?

I have also raised an issue/question for this on the atom/electron GitHub repo.


Solution

  • The suggestion from another related issue's comments to add an environment variable of {ATOM_SHELL_INTERNAL_RUN_AS_NODE : 0} helped to successfully resolve the issue with using fork in place of spawn

    More details can be seen on the GitHub issue