title: Passing require.main to path.dirname
body: <p>I'm trying to figure out why </p>
  
  <p><code>path.dirname(require.main)</code></p>
  
  <p>returns <code>.</code> or the path from the root of my application to the main module running, which in this case are the same—I'm running a gulp task defined in <code>gulpfile.js</code> at the root of my application.</p>
  
  <p>The <a href="https://nodejs.org/api/path.html#path_path_dirname_p" rel="nofollow">docs</a> say that <code>path.dirname</code></p>
  
  <blockquote>
    <p>Return the directory name of a path. Similar to the Unix dirname command.</p>
  </blockquote>
  
  <p>but <code>require.main</code> is a module object itself, not a path.  Is this an undocumented feature of <code>path.dirname</code>?</p>
  
  <hr>
  
  <p>Further information</p>
  
  <p>For what it's worth, <code>require.main.filename</code> is a deep path to my global <code>gulp</code> install, which is nowhere near what I'm looking for.</p>
  
  <p>I'm doing this so I can get a relative path <strong>to</strong> a root level <code>controllers</code> directory <strong>from</strong> my current node module, which might happen to be installed at any number of locations.  The final code to do this looks like this: </p>
  
  <p><code>path.relative(__dirname, path.resolve(path.dirname(require.main), "controllers"))</code></p>
  
  <p><em>which works</em>, the purpose of this question is just to understand how <code>path.dirname</code> is working here, since it seems to be different than what the docs say.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>All that said, it looks like <code>path.resolve('.')</code> will in fact map out the path to my application root, <code>path.resolve('.', 'controllers')</code> will map out the path to my root controllers directory, and <code>path.relative(__dirname, path.resolve('.', "controllers"))</code> will map out the relative path from the current location to said controllers directory. </p>
  
  <p>So all of this was more work than needed, but I am still curious why <code>path.dirname(require.main)</code> behaves as it does, and where that's documented. </p>