title: node.js: Accessing local variables from another module
body: <h2>My Problem</h2>
  
  <p>I'm writing a <code>node</code> module called <code>a</code>, which <code>require()</code>s a module <code>b</code> (written by a stranger). Unfortunately, <code>a</code> doesn't only need to access the public members - it also needs to access local variables declared in the scope of the module.</p>
  
  <pre><code>// a
  var b = require('b');
  
  console.log(b.public);
  console.log(b.private); // undefined
  
  
  // b
  var c = require('c');
  var stdin = process.stdin;
  
  exports.public = true;
  var private = true;
  </code></pre>
  
  <h2>My Solution</h2>
  
  <pre><code>// a
  var b = require('b');
  var srcPath = require.resolve('b');
  
  console.log(b.public);
  fs.readFile(srcPath, 'utf-8', function (err, src) {
      var box = {};
      var res = vm.runInNewContext(src, box, srcPath);
      console.log(box.private);
  });
  </code></pre>
  
  <p>But <code>vm</code> doesn't run <code>b</code> as a module, so <code>require()</code> etc. aren't accessible from the context of the <code>vm</code>. So there are <code>ReferenceError</code>s like:</p>
  
  <pre><code>    var res = vm.runInNewContext(src, box, scPath);
                   ^
  ReferenceError: require is not defined
      at &lt;module b&gt;
      at &lt;module a&gt;
      at fs.readFile (fs.js:176:14)
      at Object.oncomplete (fs.js:297:15)
  </code></pre>
  
  <h2>My Question</h2>
  
  <p>Which is the cleanest way to <strong>get the value of a local variable declared in another module</strong>? Ideas?</p>
  
  <p>Thanks for your help.</p>