reqroot
v1.2.0
Published
require a module path resolved from application root dir
Downloads
6
Readme
reqroot
requiring a module path resolved from application root dir
Use in global
Define require('reqroot')
in the file run directly from Node.js.
For test, use mocha -r --require
argument as mocha -r reqroot
.
Example
app.js
app.js is called by node for example node app.js
.
require('reqroot');
const arrayUtil = reqroot('utils/array');
const Book = reqroot('models/book');
Book.fetch().then(books => {
console.log(arrayUtil.mapField(books));
});
Book.fetchNames().then(names => {
console.log(names);
});
models/book.js
reqroot can be used directly in relative modules because global.reqroot is defined at the time required in app.js.
const arrayUtil = reqroot('utils/array');
const books = [{ name: 'A boy'}, { name: 'Ito' }];
exports.fetch = () => {
return Promise.resolve(books);
}
exports.fetchNames = () => {
return Promise.resolve(arrayUtil.mapField(books, 'name'));
}
Use in a module
any sub modules, not run file
const reqroot = require('reqroot');
const arrayUtil = reqroot('utils/array')
...