npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

require.all

v2.0.5

Published

Apply filters and require all files in a directory or read all files from a dir and optionally resolve them, as much times as you wish, with custom function or arguments.

Downloads

474

Readme

require.all - function([dirname, options])

Apply filters and require()/read all files within a specified directory and optionally resolve them with custom function or arguments.

Usage

$ npm install --save require.all
var controllers = require('require.all')('./controllers');

// same as
var controllers = {
    homeController: require('./controllers/home-controller.js'),
    aboutController: require('./controllers/about-controller.js'),
    admin: {
        dashboardController: require('./controllers/admin/dashboard-controller.js')
    }
    // ...
}

Advanced

require.all = require('require.all');

var controllers = require.all({
    dir: './controllers',
    match: /controller\.js$/i, //only files that end with 'controller.js'
    recursive: false,

    // rename module: use default mapping function and remove 'controller' from the end
    map: (name, path, isFile) => require.all.map(name, path, isFile).replace(/controller$/i, '')
});

// resolve all controllers to have their instances
controllers(function(name, Controller){
    this === controller; // true
    return new Controller();
});

// the above code does the same as
var controllers = {
    home: new require('./controllers/home-controller.js')(),
    about: new require('./controllers/about-controller.js')()
    // ...
};

Options and defaults

require.all = require('require.all');

var modules = require.all({
        dir:        '.',
        not:        /^\./,
        match:      null,
        map:        map,
        ignore:     /^\.|node_modules/,
        require:    /\.(js|json)$/,
        recursive:  true,
        encoding:   'utf-8',
        tree:       true
})

Option | Type | Default | Description -|-|:-:|- dir | String | '.' | Absolute or relative path to the directory to be traversed. require.all will not require it's parent file to prevent infinite loops. Default: current directory not|RegExp/Function(name)/null| /^\./ | Filter to specify which files to be skipped. Use null to disable. Default: skip files beginning with '.' match | RegExp/Function(name)/null | null | Only matching files will be required. Use null to disable. map| Function(name, path, isFile) | require.all.map | Function to be used for renaming files and directories. Receives node's name and path and a flag which is true if the node is a file (false if dir). Must return the new name to be used. Default: convert to camelCased ignore| RegExp/Function(name)/null | /^\.|node_modules/ | Filter to specify which child directories to be ignored. Applied only in recursive mode. Use null to disable. Default: ignore node_modules and folders beginning with . require | RegExp/Function(name)/null | /\.(js|json)$/ | Filter to specify which files shall be loaded using require. Files that do not pass this filter will be loaded as a string. Use null to read all files as string. Default: only .js and .json files will be require-d recursive | Boolean | true | Specifies whether to traverse child directories too. encoding | String | 'utf-8' | The encoding to use for the files that will be read as string. tree | Boolean | true | Determines whether the output object should mimic the structure of the files and folders, keeping the nesting level. If set to false all files will be on the same level.

WARNING: Nodes (files and dirs) with same names on the same level will overwrite each other. If tree option is set to false directory names don't matter but keep in mind that all files are loaded on the same level so they all must have unique names.

WARNING: File names and dirnames must not resolve (after the map function if any) to one of the following: name, arguments, caller, length

WARNING: Currently nested calls to require.all are not supported - you cannot use require.all to require a file that uses require.all itself. See this issue

Resolve

require.all() actually returns a function which may be used to resolve all modules. It acts much like Array.prototype.map, looping trough all the loaded modules (see examples below!). It may be applied as many times as you wish in two possible modes:

  • If you pass no parameter or an array of parameters only the modules that are functions will be executed. If the return value is none falsy it will be the new value that will be available in the returned from require.all() object (function).
var modules = require.all('./modules');
// assume we have module foo
modules.foo; // function foo(){return function bar(a){ return a}}
// execute all functions (resolve foo with no arguments)
modules();
// foo is now bar
modules.foo; // function bar(a){ return a}
// lets resolve bar
modules();
modules.foo; // function bar(a){ return a}
// bar was resolved but since `a` was undefined modules.foo is still `bar`
// lets resolve with array of none falsy values, that shall be passed as arguments 
modules(["some none falsy value", "pointless"]);
// modules.foo returns `a`, the first argument, which now is none falsy so:
modules.foo; // "some none falsy value"
  • You may pass a single function and deal with the modules as you wish. The function is applied in the context of the module itself and receives the name of the module and the module as arguments.
var modules = require.all('./modules');
modules.foo; //function foo(){return true}
modules(function(name, module){
    this === module; // true
    return module
})
modules.foo;// function foo
//again we may keep on resolving
modules()()()()();
//modules.foo is now true since we resolved it in mode 1 and it returns true
modules.foo; // true

Notice:

require.all()(1,2,3);
//same as 
require.all()([1,2,3]);
//same as 
require.all()(funnction(name, mdl){
    return mdl(1,2,3)
});
// so you may pass arguments as normal 
// but if you want to pass a SINGLE argument 
// which is an ARRAY or FUNCTION you should do this:
var arr = [], f = function(){};
require.all()([arr]);
require.all()([f]);
// or 
require.all()(arr, undefined);
require.all()(f, undefined);

Complete example

require.all = require('require.all');
var express = require('express');
var app = express(),
    cfg = require.all('./config/'),
    controllers = require.all('./controllers'),
    models = require.all('./models'),
    routes = require.all('./routes');
    
//pass models to controllers
controllers([models]); // models is function!

Object.assign(app, {controllers, models});

// assume we have model for each page
app.use('/:page*', function(req, res, next){
    var page = req.params.page;
    res.model = models[page];
    next();
});

// pass the controllers to all routes
routes(controllers, undefined); // controllers is also function!

// now the routes are ready to be bound;
routes(function(name, route){
    return app.use('/' + name, route), route;
});

// our app is ready that quickly... :)
app.listen(cfg.port, cfg.host, ()=>{
    console.log('App is running on %s:%s', cfg.port, cfg.host);
});

Hey, if something is missing or you want to suggest improvements and features, you are welcome.