Running an electron process as plain node process? [duplicate]
Asked Answered
B

2

6

I have my packaged electron app using electron-packager and I want to run this app in any mac which doesn't have node installed. I was suggested that electron-packager bundles the node into my app, but when I try launching it on a mac I get the 'node command not found error'.

I get this because I invoke a child process in my application that executes a node command to run a script. In electron slack, I was suggested to run my electron process as plain node process by setting the environment variable ELECTRON_RUN_AS_NODE. I cannot figure out where and how I can set this, any idea on how to do this? Also, is this going to solve the issue?

Bolding answered 7/8, 2018 at 15:6 Comment(0)
B
8

One can use 'fork' method to run a node process and this even works on a machine with no node installed. 'Fork' method uses the executable path of the parent process in this case electron app. The sample code for fork method is given below:

const child = childProcess.fork(path, args, {
    silent: true,
    detached: true,
    // stdio: 'ignore',
    env: {
        ELECTRON_RUN_AS_NODE:1
    }
});

Also set the 'ELECTRON_RUN_AS_NODE' env variable. This worked for me and I was able to run the app on a mac with no node installed.

Bolding answered 28/8, 2018 at 20:46 Comment(0)
A
3

Alright, so your problem is this: Electron packages nodeJS to work only within the scope of that application. When spawning additional processes you are telling the OS that it must use "node" installed on the OS, not the node bundled with electron. So, if node is not installed on that system, you cannot call node to run a script.

go have a look at the pkg module. What is does, is take a script and bundles node into an executable and then you can refer to that when spawning a process.

Althaalthea answered 8/8, 2018 at 18:38 Comment(6)
Do you mean that even after setting the ELECTRON_RUN_AS_NODE variable my app still requires node in the system? The electron documentation only says that setting that variable makes the process to run as a node process instead of electron process. Does this mean that node still needs to be installed on the system? Isn't there a way we can ask the child process to fetch node bundled with the packaged app?Bolding
@Bolding if you invoke a process like "node start" you will need node in the system, electron only will solve the issue related to have nodejs just because you are running an electron app.Creodont
@Bolding , unfortunately not. It would have saved me tons of time if we could though.Althaalthea
I don't think we need node again. Check the link #51772254Bolding
You know what, I now need to test this. Let me see if I can do this and I will put up a gist once I am done. Just give me a few. Its late night here in SA.Althaalthea
@ClaytonBez any luck?Bolding

© 2022 - 2024 — McMap. All rights reserved.