bytenode/bytenode

forking a compiled script from an electron production app

wgerven opened this issue ยท 5 comments

In my case, most of my business logic is run in a separate process, which I bundle into a single js file and then fork from the renderer.

In development I've got forking working, taking care that I compile the js against electron locally available. I also use the "trick" as per #103 (comment)

However, where it gets hairy is in production, i.e., after bundling the whole things using electron-builder. I am aware that the js file should be compiled against the electron version cached by electron-builder, which is not the one locally available under node_modules/electron but kept in a cache folder (see here: https://github.com/electron/get#how-it-works).

I also kept the bytenode module among those which are packaged into the asar archive.

However, none of this seems to work, it's not forking the process. I think it's because the bytenode executable cannot be executed to create the child process since it's spawned with node which is (I guess?) not locally available in production. edit: No, the problem is spawning a forked process using bytenode from "within" the asar.

Is there some way to make this work?

Ps: Please forgive me for this one https://github.com/bytenode/bytenode/issues/182, this was a mistake

jjeff commented

Have you tried without ASAR enabled? Things can get weird putting binaries in the asar file.

Hi @jjeff, thanks for your feedback. Yes, you're right, I can confirm that with asar turned off it works good.
However, electron-builder notifies me:

asar usage is disabled โ€” this is strongly not recommended solution=enable asar and use asarUnpack to unpack files that must be externally available

I'd myself too rather keep asar enabled. Is there perhaps a way that we could leverage asarUnpack to achieve the same result? I highly appreciate your thoughts.

Ok, I've got it working myself without disabling asar.

In electron-builder.json, instead of asar: false I put

"asarUnpack": [
    "node_modules/bytenode"
  ]

This will keep the bytenode module outside of the asar. Then to fork the compiled process I can use

cp.fork(
    path.resolve(appPath, 'something_hidden.jsc'), // this is contained within the asar
    [],
    {
        execArgv: ['-r', path.resolve(appPath, '..', 'app.asar.unpacked', 'node_modules', 'bytenode')],
        env: {...}
    }
)

Now just to automate and clean up the build process.

jjeff commented

Yeah. I just meant to try it without asar to see if that was the source of the problem. I think you're on the right track with asarUnpack.

But, for anyone listening in, it is possible to keep your bytenode .jsc files in your asar file.

The source of this problem may have more to do with the subprocess trying to access the asar rather than something bytenode-specific.

Yes I agree, that's why I documented how I achieved it and emphasized that the jsc is still in the asar. Thanks for your input, it put me in the right direction.