It requires files from the base path of your app and let you configure alias to other paths for faster refactoring. No more ../..
!!
- No bootstrap required
- No dependencies
- Multiple alias support
- Local path file resolution
- Proxy support (mocks)
$ npm install xreq --save
Use it in your modules:
//Current js file at foo/bar/a.js
var xreq = require('xreq');
//Require a file at other/folder/b.js, only searh from the base path
var b = xreq('other/folder/b');
It searches the package.json
of your project, and build routes from that point.
Add some custom paths at package.json
"xreq": {
"server": "src/server",
"services": "src/server/services",
"models": "src/server/models",
"test": "test"
}
Use it in your modules:
//Current js file at foo/bar/a.js
var xreq = require('xreq');
//Require a file at "src/server/services". Use "service" alias as in the xreq
var AService = xreq.services('AService');
// That is better than require('../../src/server/services/AService');
Add a second parameter with value true
var xreq = require('xreq');
// Print the base location, the folder where the xreq file resides.
console.log(xreq('.', true));
// Print the complete path to file a.hbs
console.log(xreq('a.hts', true));
// It works the same way for aliases
console.log(xreq.templates('products.hts', true));
This method is quite useful for resolve template location or other static files in the local application.
xreq(function (file) {
return require('another_folder/' + file);
});
The following line use the proxy function.
var bar = xreq('foo.js');
If the proxy function returns a falsy value the resolution continues normally.
xreq(null);