title: NodeJS not spawning child process except in tests
body: <p>I have the following NodeJS code:</p>
  
  <pre><code>var spawn = require('child_process').spawn;
  
  var Unzipper = {
    unzip: function(src, dest, callback) {
      var self = this;
  
      if (!fs.existsSync(dest)) {
        fs.mkdir(dest);
      }
  
      var unzip = spawn('unzip', [ src, '-d', dest ]);
  
      unzip.stdout.on('data', function (data) {
        self.stdout(data);
      });
  
      unzip.stderr.on('data', function (data) {
        self.stderr(data);
  
        callback({message: "There was an error executing an unzip process"});
      });
  
      unzip.on('close', function() {
        callback();
      });
    }
  };
  </code></pre>
  
  <p>I have a NodeUnit test that executes successfully.  Using phpStorm to debug the test the var <em>unzip</em> is assigned correctly</p>
  
  <p><img src="https://i.stack.imgur.com/vOkO1.png" alt="phpStorm debug screenshot of test"></p>
  
  <p>However if I run the same code as part of a web service, the <em>spawn</em> call doesn't return properly and the server crashes on trying to attach an <em>on</em> handler to the nonexistent <em>stdout</em> property of the <em>unzip</em> var.</p>
  
  <p><img src="https://i.stack.imgur.com/ly6sq.png" alt="enter image description here"></p>
  
  <p>I've tried running the program outside of phpStorm, however it crashes on the command line as well for the same reason.  I'm suspecting it's a permissions issue that the tests don't have to deal with.  A web server spawning processes could cause chaos in a production environment, therefore some extra permissions might be needed, but I haven't been able to find (or I've missed) documentation to support my hypothesis.</p>
  
  <p>I'm running v0.10.3 on OSX Snow Leopard (via MacPorts).</p>
  
  <p>Why can't I spawn the child process correctly?</p>
  
  <p><strong>UPDATES</strong></p>
  
  <p>For @jonathan-wiepert</p>
  
  <p>I'm using <a href="http://howtonode.org/prototypical-inheritance" rel="nofollow noreferrer">Prototypical inheritance</a> so when I create an "instance" of Unzipper I set stdout and stderr ie:</p>
  
  <pre><code>var unzipper = Unzipper.spawn({
    stdout: function(data) { util.puts(data); },
    stderr: function(data) { util.puts(data); }
  });
  </code></pre>
  
  <p>This is similar to the concept of "constructor injection".  As for your other points, thanks for the tips.</p>
  
  <p>The error I'm getting is:</p>
  
  <pre><code>project/src/Unzipper.js:15
      unzip.stdout.on('data', function (data) {
                   ^
  TypeError: Cannot call method 'on' of undefined
  </code></pre>
  
  <p>As per my debugging screenshots, the object that is returned from the <em>spawn</em> call is different under different circumstances.  My test passes (it checks that a ZIP can be unzipped correctly) so the problem occurs when running this code as a web service.</p>