ajsie
ajsie

Reputation: 79716

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?

Upvotes: 2

Views: 181

Answers (1)

loganfsmyth
loganfsmyth

Reputation: 161477

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()

Upvotes: 1

Related Questions