title: Why node spawn() returns "undefined" at the beginning
body: <p>Using Node's <a href="https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options" rel="nofollow noreferrer">spawn()</a> function always returns &quot;undefined&quot; at the beginning, like in the following real example:</p>
  <blockquote>
  <p>undefined M text.txt M text_2.txt</p>
  </blockquote>
  <p>This is the command <code>git status --porcelain</code> on a testing repository, this same command executed on the command line interface shows <em>(command included)</em>:</p>
  <blockquote>
  <p>$ git status --porcelain</p>
  <p>M text.txt</p>
  <p>M text_2.txt</p>
  </blockquote>
  <p>The <code>spawn()</code> events are written like this:</p>
  <pre><code>  spawn.stdout.setEncoding('utf8');
    spawn.stderr.setEncoding('utf8');
  
    spawn.stdout.on('data', function(d){ 
      data += d;
    });
    spawn.stderr.on('data', function(e){
      err += e;  
    });
  
    spawn.on('exit', function(){
      callback(err, data);
    });
  </code></pre>
  <p>Why is <code>spawn()</code> returning &quot;undefined&quot; at the beginning and how do I solve it?</p>