Note that there are some explanatory texts on larger screens.

plurals
  1. PORunning a shell command from Node.js without buffering output
    primarykey
    data
    text
    <p>I'm trying to launch a shell command from Node.js, <em>without</em> redirecting that command's input and output -- just like shelling out to a command using a shell script, or using Ruby's <code>system</code> command. If the child process wants to write to STDOUT, I want that to go straight to the console (or get redirected, if my Node app's output was redirected).</p> <p>Node doesn't seem to have any straightforward way to do this. It looks like the only way to run another process is with <code>child_process</code>, which <em>always</em> redirects the child process's input and output to pipes. I can write code to accept data from those pipes and write it to my process's STDOUT and STDERR, but if I do that, the APIs force me to sacrifice some flexibility.</p> <p>I want two features:</p> <ul> <li>Shell syntax. I want to be able to pipe output between commands, or run Windows batch files.</li> <li>Unlimited output. If I'm shelling out to a compiler and it wants to generate megabytes of compiler warnings, I want them all to scroll across the screen (until the user gets sick of it and hits Ctrl+C).</li> </ul> <p>It looks like Node wants to force me choose between those two features.</p> <ul> <li>If I want an unlimited amount of output, I can use <a href="http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options"><code>child_process.spawn</code></a> and then do <code>child.stdout.on('data', function(data) { process.stdout.write(data); });</code> and the same thing for <code>stderr</code>, and it'll happily pipe data until the cows come home. Unfortunately, <code>spawn</code> doesn't support shell syntax.</li> <li>If I want shell syntax, I can use <a href="http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback"><code>child_process.exec</code></a>. But <code>exec</code> insists on buffering the child process's STDOUT and STDERR for me and giving them to me all at the end, and it limits the size of those buffers (configurable, 200K by default). I can still hook the <code>on('data')</code> events, if I want to see the output as it's generated, but <code>exec</code> will still add the data to its buffers too. When the amount of data exceeds the predefined buffer size, <code>exec</code> will terminate the child process.</li> </ul> <p>(There's also <a href="http://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback"><code>child_process.execFile</code></a>, which is the <em>worst</em> of both worlds from a flexibility standpoint: no shell syntax, but you still have to cap the amount of output you expect.)</p> <p>Am I missing something? <strong>Is there any way to just shell out to a child process in Node, and <em>not</em> redirect its input and output?</strong> Something that supports shell syntax and doesn't crap out after a predefined amount of output, just like is available in shell scripts, Ruby, etc.?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload