title: nodejs: nedb filename path causing problems; how to fix?
body: <p>I'm an experienced programmer, but relatively new to nodejs, nedb, Mocha.</p>
  
  <p>nedb is a nodejs npm module for small data usages, that replicates some of the interface used in Mongodb.  I chose it because I'm building an app to run on a Beaglebone Black or Raspberry Pi and didn't want the overhead for Mongodb for my small data needs.</p>
  
  <p>However, I'm having a problem with the path portion of the filename for nedb.  It works fine if the myapp.js I'm working on is invoked local to the current path, but if I invoke it from a different directory, like my Mocha test directory, then nedb won't work with the same path.  It won't work with relative path either; I have to specify the full path, starting with /home before I can get it to work from the Mocha test directory.  Needless to say, this is both inconvenient, and prohibitave for production, as it's not running with a base of /home in production.</p>
  
  <p>In the code that follows, myapp.js is the application I'm developing and testing.  useMyapp.js is a utility test to use myapp.js for simplicity.  These are all in the same directory, and work properly when I invoke useMyapp.js.</p>
  
  <p>myapp.js is located in /home/geek/project/public/javascripts/</p>
  
  <p>Here is the code for myapp.js:</p>
  
  <pre><code>var Datastore = require("nedb");
  var db = new Datastore({filename: "./calEvents.db", autoload: true});
  
  function Myapp(year, month) {
      console.log('myapp: year: ' + year + ', month: ' + month);
      this.year = year;
      this.month = month;
  }
  
  Myapp.prototype.getAllData = function() {
      db.find({}, function(err, docs) {
          if(err) {
              console.error('error: ', err);
          } else {
              console.log(" docs found: ", docs);
          }
      });
  };
  
  module.exports = Myapp;
  </code></pre>
  
  <p>useMyapp.js is located in the same directory at /home/geek/project/public/javascripts/</p>
  
  <p>Here is the code for useMyapp.js, which works correctly, outputing the expected nedb records.</p>
  
  <pre><code>var Myapp = require("./myapp");
  
  var myapp = new Myapp(2016, "Mar");
  
  myapp.getAllData();
  </code></pre>
  
  <p>I copied useMyapp.js to my Mocha test directory, renamed it to be TestUseMyapp.js, into /home/geek/project/test/
  I adjusted the require statment for relative path to myapp.js
  Here's the contents of TestUseMyapp.js in /home/geek/project/test/</p>
  
  <pre><code>var Myapp = require("../public/javascripts/myapp");
  
  var myapp = new Myapp(2016, "Mar");
  
  myapp.getAllData();
  </code></pre>
  
  <p>The output from running this via nodejs is an empty data set:
      docs found: []</p>
  
  <p>THE PROBLEM:
  TestUseMyapp.js is able to find and load myapp.js per the 'require' statement, but the filename parameter for nedb is not working when myapp.js is invoked from a different directory.  This is causing me problems between development, Mocha testing, and running in production, as the file path are all different.</p>
  
  <p>I created a duplicate of myapp.js, named it myapp2.js, and kept it in the same directory as myapp.js at /home/geek/project/public/javascripts.  In this, I changed the path for the nedb filename parameter.
  Here's the code in myapp2.js PLEASE NOTE THE ONLY CHANGE IS TO THE filename PARAMATER PATH:</p>
  
  <pre><code>var Datastore = require("nedb");
  var db = new Datastore({filename: "/home/geek/project/public/javascripts/calEvents.db", autoload: true});
  
  function Myapp(year, month) {
      console.log('myapp: year: ' + year + ', month: ' + month);
      this.year = year;
      this.month = month;
  }
  
  Myapp.prototype.getAllData = function() {
      db.find({}, function(err, docs) {
          if(err) {
              console.error('error: ', err);
          } else {
              console.log(" docs found: ", docs);
          }
      });
  };
  
  module.exports = Myapp;
  </code></pre>
  
  <p>This works as expected when I invoked the test against myapp2.js with the full path.</p>
  
  <p>THE QUESTION:
  How do I resolve the filename path issue with nedb so that I don't have to change it between development, Mocha testing, and deployment to production?</p>
  
  <p>Thanks!</p>