glob-module-loader

A library to load all files as modules based on glob syntax.

glob-module-loader for node.js

A node.js library to find and load files as modules based on glob file syntax (i.e. 'plugins/*/.js').

var loader = require('glob-module-loader');
 
// a single path with a single callback once all modules are loaded 
loader.load('plugins/**/*.js', function(errmodules) {
  console.log('Loaded %s' modules.length);
});
 
// multiple paths and a callback that should be called for each module loaded 
loader.load(
  ['plugins/**/*.js', 'vendor/**/*.js'],
  function(module) { console.log('Loaded a module'); },
  function(errmodules) { console.log('Loading complete'); }
);

glob-module-loader can be easily installed using npm.

npm install glob-module-loader

Then simply require glob-module-loader in your application

require('glob-module-loader');

glob-module-loader is intended to find all files matching a glob syntax (i.e. "plugins/*/.js") and load each file as an individual module. It can load all modules synchronously and hand back an array, or it can load them asynchronously calling a callback method for each module loaded.

var loader = require('glob-module-loader');
 
// load synchronously 
loader.load('plugins/**/*.js', function(errmodules) {
  console.log('Loaded %s' modules, modules.length);
});
 
// load asynchronously 
loader.load('plugins/**/*.js',
  function(module) {
    console.log('Called for each module loaded');
  },
  function(errmodules) {
    console.log('Loaded %s' modules, modules.length);
  });

glob-module-loader depends on the following packages:

Searches the file system for modules to load based on the glob patterns provided. Once all modules have been loaded the callback function is called passing the loaded modules as an array.

Arguments

  • patterns - A string or array of strings containing glob patterns to search.
  • module_callback(module) - An optional callback function which is called for each module immediately after it is loaded.
  • callback(err, modules) - A callback which is called after all modules have been loaded, or until an error is encountered. If an error is encountered the callback will be called immediately and no modules will be returned.

Example

loader.load(['*.js', 'dir/*.js'], function(errmodules){
  // all modules loaded are now in the modules array 
});
 
loader.load(
  ['*.js', 'dir/*.js'],
  function(module) {
    // called for each module 
  },
  function(errmodules){
    // all modules loaded are now in the modules array 
});