Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript. Learn more »

mod.js

2.0.0 • Public • Published

mod.js

Build Status A Rust inspired utility tool for loading modules from a directory. Much like the mod.rs file that handles the repsonsibility or exporting modules inside a directory, mod.js attempts to take on a similar responsibility with minimal overhead.

Getting Started

First, install through npm

npm install --save mod.js

Then, just require:

ES5

var load = require('mod.js');

ES6

import load from 'mod.js';

Examples

load

Given the directory

├ schemas/
│   ├ index.js
│   ├ person.js
│   ├ house.json // Supports .js and .json
├ index.js

Then in your schemas/index.js file, you can simply

// ES5
var load = require('mod.js');
 
module.exports = load(__dirname);
 
---
 
// ES6
import load from 'mod.js';
 
export default load(__dirname);

And then it's as easy as

// root index.js
var schemas = require('./schemas');
// {
//    person: { ... },
//    house: { ... }
//}

loadAsync

There is another function, loadAsync, that will load the files asynchronously and return a promise.

Given the same directory as above, your schemas/index.js file should look something like:

// ES5
var loadAsync = require('mod.js').loadAsync; 
 
module.exports = loadAsync(__dirname);
 
---
 
// ES6
 
import { loadAsync } from 'mod.js';
 
export default loadAsync(__dirname);

And once again:

// root index.js
require('./schemas')
    .then(schemas => {
        console.log(schemas); // { person: { ... }, house: { ... } }
    });

install

npm i mod.js

Downloadsweekly downloads

1

version

2.0.0

license

ISC

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability