Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Require a directory of node.js modules as a flat or nested object.
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.


dire (DIrectory REquire) is a simple node.js module capable of requiring an entire directory of other modules.


  • requiring a directory with one statement, either as a flat (one level) or nested (namespaced) object;
  • filtering modules by file extension (i.e. you can require just the .json files or just the .js files).


npm install dire


var dire = require('dire');
var x = dire('/path/to/some/directory/');

This is the simplest use case. It requires any loadable node.js files in that directory and returns a nested object. If /path/to/some/directory had the following structure:

'-- a/
    '-- cfg.json
    '-- mod.js
'-- b/
    '-- cfg.json
    '-- mod.js
    '-- err.js
    '-- completelyrandommodule.js

the object x would be:

    a: {
        cfg: [...],
        mod: [...]
    b: {
        cfg: [...],
        mod: [...],
        err: [...],
        completelyrandommodule: [...]

dire can also be told to load a flat object:

var x = dire('/path/to/some/directory/', true);

and then x would be:

    'a/cfg': [...],
    'a/mod': [...],
    'b/cfg': [...],
    'b/mod': [...],
    'b/err': [...],
    'b/completelyrandommodule': [...]

The third parameter provided to dire specifies the file extension filter:

var x = dire('/path/to/some/directory/', false, '.json');

The value of x would now be:

    a: {
        cfg: [...]
    b: {
        cfg: [...]


This software is available under the terms of the MIT License.


This module is largely inspired by Troy Goode's node-require-directory

Thanks to @romanshestilovski for spotting and fixing the local path bug.

Bugs/Feature requests

You gotta be kidding. There is no possibility in this universe of a software such as this to have any bugs. You are entitled, however, to at least try to prove me wrong. I hereby permit you to do that using the issue tracker or by sending an email to xD

Something went wrong with that request. Please try again.