No sign of life from node.js child process

Hi all,

in my program I have to work with a Node.js script. The script has to be executed. Itself writes some data into a data base and prints some data to stdout and some information to stderr. In order to start the script I use:

let child = Command::new(node)
    .arg("my_script.js")
    .stdout(Stdio::piped())
    .stderr(Stdio::inherit())
    .spawn()?;

The problem is that node doesn't seem to start. No data is writen into the data base nor do I receive bytes at child.stdout nor do I see some output on the console. However the node process is still running. If I launch normal binaries everythings works fine, so this seems to be related to node.js but I have no experience with it.

Has anyone an idea what I do wrong?

If it was failing to start, then it could be a case of node executable not being found (in terminal it uses PATH, but Command needs to have absolute path).

Does it work if you don't redirect stdout/stderr? I some programs I had a deadlock caused by child process waiting for its stdout buffer to be emptied by the parent process, but the parent process was waiting for the child to finish, so it wasn't reading it.

2 Likes

Yep, that was the one point, thank you for the advice.

The other error I found was due to my own fault. For documentation reasons: I passed sevreal Strings as arguments to the Command like:

args_string.split(' ').for_each(|arg| cmd.arg(arg));

The problem was when passing in an emypty String it added an argument "" but node seams to interpret the first argument as the script and in my case I called $> node '' my_script.js so nothing was executed...

This was solved by:

args_string.split(' ').filter(|arg| !arg.is_empty()).for_each(|arg| cmd.arg(arg));
2 Likes