title: How do I get the dirname of the calling method when it is in a different file in nodejs?
body: <p>Let's say I have two files, <code>dir/a.js</code> and <code>lib/b.js</code></p>
  
  <p>a.js:</p>
  
  <pre><code>b = require('../lib/b');
  b.someFn();
  </code></pre>
  
  <p>b.js:</p>
  
  <pre><code>var fallback = "./config.json";
  module.exports = {
    someFn = function(jsonFile) {
      console.log(require(jsonFile || fallback);
    }
  }
  </code></pre>
  
  <p>The entire purpose of <code>b.js</code> in this example is to read a json file, so I might call it as <code>b.someFn("path/to/file.json")</code>. </p>
  
  <p>But I want there to be a default, like a config file. But the default should be relative to <strong>a.js</strong> and not <strong>b.js</strong>. In other words, I should be able to call <code>b.someFn()</code> from <code>a.js</code>, and it should say, "since you didn't pass me the path, I will assume a default path of <code>config.json</code>." But the default should be relative to <code>a.js</code>, i.e. should be <code>dir/config.json</code> and <strong>not</strong> <code>lib/config.json</code>, which I would get if I did <code>require(jsonFile)</code>.</p>
  
  <p>I could get the <code>cwd</code>, but that will only work if I launch the script from within <code>dir/</code>. </p>
  
  <p>Is there any way for <code>b.js</code> to say, inside <code>someFn()</code>, "give me the <code>__dirname</code> of the function that called me?"</p>