3

I've an issue with Node.js. With Python, if I wanted to execute an external command, I used to do something like this:

import subprocess
subprocess.call("bower init", shell=True)

I've read about child_process.exec and spawn in Node.js but I can't do what I want. And what's I want?

I want to execute an external command (like bower init) and see its output in real time and interact with bower itself. The only thing I can do is receive the final output but that don't allow me to interact with the program.

Regards

Edit: I saw this question but the answer doesn't work here. I want to send input when the external program needs it.

Community
  • 1
  • 1
Henrique Dias
  • 182
  • 4
  • 13
  • possible duplicate of [How to run interactive shell command inside node.js?](http://stackoverflow.com/questions/27458502/how-to-run-interactive-shell-command-inside-node-js) – Scimonster Jun 13 '15 at 20:32
  • That answer shows how to read data and how to send data. It's not clear what your edit is asking for that isn't covered by that. You'd listen for data from the process, and call `.write` to send data to the process. – loganfsmyth Jun 13 '15 at 20:41
  • I'm going to try again. It appears to be simple but.. – Henrique Dias Jun 13 '15 at 20:43
  • If you *really* need a shell, then take a look at [pty.js](https://github.com/chjj/pty.js/). Otherwise, [`child_process.spawn()`](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) should be all that you need. – hexacyanide Jun 13 '15 at 21:21

1 Answers1

3

How about this?

var childProcess = require('child_process');

var child = childProcess.spawn('bower', ['init'], {
  env: process.env,
  stdio: 'inherit'
});

child.on('close', function(code) {
  process.exit(code);
});

Seemed to work for me

Travis Kaufman
  • 2,867
  • 22
  • 23