I'm trying to execute a machine learning script from a node.js application, using the child-process core module as explained here
However, I can't get a simple output from script.stdout.on.
I'm using Node v12.5.0 and python 3.7.3
I made a sample "hello world" script and JS function to test (shown below), and ran the script from command line just fine.
These are my scripts:
test.py
import sys
print('hello world')
sys.stdout.flush()
JavaScript test function
const { spawn } = require("child_process");
const pytest = spawn("python", ["./path/to/test.py"]);
console.log("firstPrint");
pytest.stdout.on("data", data => {
console.log("secondPrint");
console.log(data);
});
pytest.stdout.on("close", code => {
console.log(`script closed: ${code}`);
});
My console output is:
firstPrint
script closed: false
Although it should be:
firstPrint
secondPrint
hello world
script closed: {code}