0

I've found an issue that I've never seen before. I've tried to run a nodejs process through NodeJS's builtin Child_Process module. I achieved it like this:

const child_process = require('child_process');
const proc = child_process.exec("node test.js");
proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);
proc.on('exit', code => console.log("Process exited with code", code));

The interesting thing though, that the process exits with a non-zero exit code although the process runs fine in a standard terminal session. In this case I'm using Bash.

The script looks like this:

for (const i of new Array(10).fill(0).map((a, i) => i))
    console.log(i ** 2);

It prints the square of the numbers 0 - 10.

I've written the script in Bash and Python and each produce the correct result. The output is printed to process.stdout.

I can run the JavaScript example in Deno as well, however, the result is the same as in Node.

I've never seen this behaviour before and the docs don't seem to have much of a solution to this. The closest I got was a suggestion stating to access the streams through proc.stdio, which also produced the same result.

J-Cake
  • 1,526
  • 1
  • 15
  • 35
  • It works well in repl.it https://repl.it/repls/DefensiveAssuredHarddrive try this out. I have 2 ideas which you might consider: 1. Ensure that you are using supported features by your current node version. 2. You have proper access to execute other file. Also: 1. Can you tell me what node version are you using? 2. Can you tell me how do you launch your main process? – Žilvinas Jocius Jul 23 '20 at 09:27
  • Hi, My node version is `v15.0.0-nightly20200618a4f3206b76` and I'm running the main process with `node ./test_runner.js` in a standard bash session through a terminal. It might be worth noting that I'm running Ubuntu 20.04\ – J-Cake Jul 23 '20 at 12:14

0 Answers0