1.0.4 • Published 9 years ago

dir-require v1.0.4

Weekly downloads
7
License
ISC
Repository
github
Last release
9 years ago

dir-require


Require a directory of js modules.

Build Status Coverage Status license MIT

Install

$ npm install --save dir-require

Examples

require other modules in index.js when the file tree is like

.
├── index.js
├── testModule1.js
└── testModule2.js

your js code in index.js

const models = require('dir-require').load(__dirname, {ignore: 'index.js'}); // 1st argument is absolute path

/*
You will get
models = {
    'testModule1': require('testModule1'),
    'testModule2': require('testModule2'),
}

*/
const models = require('dir-require').loadArray(__dirname, {ignore: 'index.js'});

/*
You will get
models = [require('testModule1'), require('testModule2')];

*/

Usage

const moduleLoader = require('dir-require');

moduleLoader.load(dirpath, options);
moduleLoader.loadArray(dirpath, options);

dirpath

should be absolute path.

options.match

  • String (only load this file)
  • Array of String (only load files in array)
  • Regx (load files match regx)

options.ignore

  • String
  • Array of String
  • Regx
  • if options.match is set options.ignore is no use.

License

MIT © liu946