reqroot

1.2.0 • Public • Published

reqroot

NPM Version Build Status

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')
 
...

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i reqroot

Weekly Downloads

1

Version

1.2.0

License

MIT

Unpacked Size

3.64 kB

Total Files

5

Last publish

Collaborators

  • tilfin