Creates a 'simple' dependency injection tree from a directory structure.
$ cnpm install inject-glob 
SYNC missed versions from official npm registry.

Build Status NPM version


inject-glob is a simple wrapper around nject and glob. glob provides a mechanism to traverse a directory structure and 'find' files for injection to create a dependency injection tree.


%> npm install inject-glob --save


This is the basic usage. In this usage, where all options are defaulted, the names are derived from file names, no aggregation of any of the injectables is done, and the default globs are used.

path  = require('path')
di    = require('inject-glob')

di(null, (err, resolved) ->

For reference, here are the defaults, where the function injectName simply removes the extension of a file and uses the file name for the name of the injectable:

defaults =
  globs         : [ '**/*.js', '**/*.coffee', '**/*.json' ]
  cwd           : '.'
  interceptName : (f) -> injectName(f)
  aggregateOn   : ->

A possibly more complete and detailed version might look like the following. In this example everything in the app/model directory is aggregated on to resolved.model. The name of the default.json file is mapped to config and so provided on resolved.config and injected in functions which need those value with the normal mechanism module.exports = (config) ->.

path  = require('path')
di    = require('inject-glob')

opts =
  globs         : [ '**/*.js', '**/*.coffee', '**/*.json' ]
  cwd           : 'app/'
  aggregateOn   : (name) ->
    switch (path.dirname(name))
      when 'app/model' then { aggregateOn: 'model' }
      else undefined
  interceptName : (name, injectName) ->
    switch (name)
      when 'config/default.json' then 'config'
      else injectName(name)

di(null, (err, resolved) ->


See license file.

The use and distribution terms for this software are covered by the Eclipse Public License 1.0, which can be found in the file 'license' at the root of this distribution. By using this software in any fashion, you are agreeing to be bound by the terms of this license. You must not remove this notice, or any other, from this software.

Current Tags

  • 0.0.4                                ...           latest (6 years ago)

4 Versions

  • 0.0.4                                ...           6 years ago
  • 0.0.3                                ...           6 years ago
  • 0.0.2                                ...           6 years ago
  • 0.0.1                                ...           6 years ago
Maintainers (1)
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (5)
Dev Dependencies (4)
Dependents (2)

Copyright 2014 - 2016 © |