title: How can I execute a node.js module as a child process of a node.js program?
body: <p>Here's my problem. I implemented a small script that does some heavy calculation, as a node.js module. So, if I type "node myModule.js", it calculates for a second, then returns a value.
  Now, I want to use that module from my main Node.JS program. I could just put all the calculation in a "doSomeCalculation" function then do:</p>
  
  <pre><code>var myModule = require("./myModule");
  myModule.doSomeCalculation();
  </code></pre>
  
  <p>But that would be blocking, thus it'd be bad. I'd like to use it in a non-blocking way, like DB calls natively are, for instance. So I tried to use child_process.spawn and exec, like this:</p>
  
  <pre><code>var spawn = require("child_process").spawn;
  var ext = spawn("node ./myModule.js", function(err, stdout, stderr) { /* whatevs */ });
  ext.on("exit", function() { console.log("calculation over!"); });
  </code></pre>
  
  <p>But, of course, it doesn't work. I tried to use an EventEmitter in myModule, emitting "calculationDone" events and trying to add the associated listener on the "ext" variable in the example above. Still doesn't work.</p>
  
  <p>As for forks, they're not really what I'm trying to do. Forks would require putting the calculation-related code in the main program, forking, calculating in the child while the parent does whatever it does, and then how would I return the result?</p>
  
  <p>So here's my question: can I use a child process to do some non-blocking calculation, when the calculation is put in a Node file, or is it just impossible? Should I do the heavy calculation in a Python script instead? In both cases, how can I pass arguments to the child process - for instance, an image?</p>