title: Running multiple independent Node programs in the same Node instance
body: <p>I'm doing embedded development on a resource-limited system, and need to run a number of separate Node.js tasks (call them <code>task1.js</code>, <code>task2.js</code> and <code>task3.js</code>). The obvious solution would be to run them separately, e.g.:</p>
  
  <pre><code>$ node task1.js &amp;
  [1] 1968
  $ node task2.js &amp;
  [2] 1969
  $ node task3.js &amp;
  [3] 1970
  $
  </code></pre>
  
  <p>This works, but I end up with three independent Node stacks, each with its own multi-megabyte heap, interpreter, etc. etc. etc., which is a waste that I'd like to avoid.</p>
  
  <p>Another obvious solution would be to concatenate the source files:</p>
  
  <pre><code>$ cat task1.js task2.js task3.js | node -
  </code></pre>
  
  <p>This works, but it has problems. First, all three task sources would end up in the same module, so I'd risk name collisions. For example, if each task file included <code>const crypto = require('crypto');</code>, then when concatenated Node would complain about the multiply-defined <code>crypto</code> variable.</p>
  
  <p>This would also require all of the primary task source files to be in the same directory, otherwise any relative path references to dependent files would be calculated based on the default working directory, and would likely break.</p>
  
  <p>So, I'm looking for a way to run multiple tasks in the same Node instance, sharing Node resources as much as possible.</p>
  
  <p>It would be great if some or all the following were true:</p>
  
  <ul>
  <li>For development/debugging convenience, the same <code>taskX.js</code> sources could be used individually (as at the top), or run at the same time in the same node instance</li>
  <li>No special care would have to be made in each task's code to prevent namespace collisions</li>
  <li>Relative path references in <code>include</code> statements wouldn't all be resolved from the same working directory, so that I could have separate source trees for the separate tasks</li>
  </ul>
  
  <p>Problems I don't need to be solved:</p>
  
  <ul>
  <li>Multiprocessing or multithreading</li>
  <li>Sharing data between the tasks</li>
  <li>Inter-task events and communication services (if I do I'll write them myself)</li>
  <li>Protecting each task from the others' bad behavior</li>
  </ul>
  
  <p>Expected constraints for the task code:</p>
  
  <ul>
  <li>No busy-waiting, so that none block the others from executing</li>
  <li>No exclusive use of common system resources (e.g. no two will open a server socket on the same port)</li>
  <li>Use of global Node resources will be restricted or forbidden</li>
  </ul>