1.2.0 • Published 5 years ago

reqroot v1.2.0

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

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

1.2.0

5 years ago

1.1.0

7 years ago

1.0.0

7 years ago