简体   繁体   中英

How to let user type in data into a spawned process on Node.js

I have a program I run programmatically on Node.js using child_process.

var spawnedProcess = childProcess.spawn(command, args);

spawnedProcess.stdout.setEncoding("utf8");
spawnedProcess.stdout.on("data", function(data) {
  return process.stdout.write(data);
});

process.stdin.pipe(spawnedProcess.stdin);

I thought this would work since everything I type into the shell would be piped into the stdin of the spawned process.

I can see what I am typing and when I hit Enter it just give me a new line. The shell isn't responding to any input.

Any clue?

As mentioned in the comments, you can potentially actually use both npm and jitsu both can potentially be used directly as modules. You may want to consider that as an option.

When you run them from the command line, all you are doing is running these two scripts:

To answer your question though, it looks totally fine except for one bit. You need to resume the stdin stream before it can be piped, as mentioned in it's docs Docs for process.stdin

You just need to add this somewhere around where you call 'pipe'.

process.stdin.resume()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM