dir-require

1.0.4 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i dir-require

Weekly Downloads

1

Version

1.0.4

License

ISC

Last publish

Collaborators

  • liu946