How to develop another module process

TL DR: How to deploy a process located outside the current current process?

I am trying to use child_process for Nodejs to start another nodejs process on the output of the parent process.

I successfully executed this process with exec , but I need the child process to be independent of the parent, so the parent can exit without waiting for the child, so I tried using spawn with the detached: true, stdio: 'ignore' option detached: true, stdio: 'ignore' and unref() process:

the options.detached to true parameter allows the child process to continue working after the parent exits.

spawn('node MY_PATH', [], {detached: true, stdio: 'ignore'}).unref();

This gives:

node MY_PATH ENOENT error. Unfortunately, I could not solve the problem.

After you had problems with spawn and read the documentation again, I decided that I should use fork :

The child_process.fork () method is a special case of child_process.spawn (), used specifically to create new Node.js processes.

fork() does not accept the command as its first argument, but modulePath , which seems to be unsuitable, since the script that I am trying to run as a child process is not in the directory of the current current process, but depending on it.

Return to start TL; DR - how to deploy a process that is outside the current running process?

Any help would be greatly appreciated!

EDIT:

Providing a spawn ENOENT error solution can also be very helpful!

+5
source share
1 answer

The following code should allow you to do what you need.

 var Path = require('path'); var Spawn = require('child_process').spawn; var relative_filename = '../../node_modules/bla/bla/bla.js'; Spawn('node', [Path.resolve(__dirname, relative_filename)], {detached: true, stdio: 'ignore'}).unref(); process.exit(0); 
+1
source

Source: https://habr.com/ru/post/1012285/


All Articles