I have an application with directories, and files in those directories reference each other via require. I may move them around, so I hate to use relative paths. Is there a way to use paths relative to my application root?
I have
/home/robert/src/application/
|\ routes
| \ foo.js
|\ models
\ bar.js
// foo.js
var bar = require('../models/bar')
, stuff = require('../other_stuff/stuff');
I want
/home/robert/src/application/
|\ routes
| \ foo.js
|\ models
\ bar.js
// foo.js
var bar = require('/models/bar')
, stuff = require('/other_stuff/stuff');
except that the paths are appended to /home/robert/src instead of evaluated on their own.
A lot of people have started creating an
index.jsfile that exports all of their modules from a single place. Modules then just require these exports from the index file instead of where the modules are actually defined. If you ever move anything around, you just need to update this one file. Plus it makes creating code coverage builds much easier.Index.js just ends up with a bunch of lines like this:
Basically this exports
appfor any module that needs it using either the standard module or the code coverage version depending on a variable.