'NodeJS child_process send input

I have a python script as follows :

print ("hello world ")
x = input("Enter a number: ")
print(x)

Now I want to programitically run the same using the below NodeJS code:

var spawn = require("child_process").spawn;

child = spawn("python", ["./test.py"]);


child.stdout.on("data", (data) => {
  console.log(`${data}`);
  child.stdin.write("7\n");
});

But the output isn't as expected and is like below:

hello world 
Enter a number: 
7

Is there any way I can send input to the node child process once the script awaiting for input?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source