title: Relative file system write path within module
body: <p>I have a executable node / javascript script that has a debug boolean, if set to true a couple of files will be written. This executable is also node module. Depending on the working directory of the user running the script it seems that the function can't find the directory to write files into.</p>
  
  <p>The module is structured like this</p>
  
  <pre><code>output/
  lib/
      helpers.js
  index.js
  </code></pre>
  
  <p>My original reasoning would be to have the path be.</p>
  
  <pre><code>helper.write = function(data,filename){
      if(typeof data !== "string") data = JSON.stringify(data);
      fs.writeFileSync("./output/"+filename, data);
  };
  </code></pre>
  
  <p>However this works when running the script from within the node_module folder</p>
  
  <pre><code>fs.writeFileSync("process.cwd()+"/node_modules/the_module/output/"+filename, data);
  </code></pre>
  
  <p>Like this</p>
  
  <pre><code>node ./my_app/node_modules/the_module/index.js
  </code></pre>
  
  <p>This gets even more confusing if the modules is used in another executable file that uses the library.</p>
  
  <pre><code>node ./my_app/run.js
  </code></pre>
  
  <p>Is there a way to save the file independent from all of these variables?</p>