Infects a dependant object with it's dependencies, i.e., a simple JS dependency injection module.

npm install infector
76 downloads in the last month


Build Status

A small JS dependency injection module for both the browser and Node.js. Inspired by the dependency injection approaches in AngularJS and node-di. No relation to Infector++.


Grab Infector

In the browser, Underscore is a dependency and Infector is exposed as a global. So do something like:

<script src="underscore.js"></script>
<script src="infector.js"></script>

Using Bower:

$ bower install infector --save

In Node.js:

$ npm install infector --save

Specify dependencies

Use a constructor property:

function Foo(bar) {}
Foo.infect = ['bar'];
// or Foo.inject = ['bar'];

Register modules

There are two return options.

  • type: an instance of type will be returned via new:
function Foo() {}
infector.register({ foo: { type: Foo } });

If you want the module to be returned as a singleton:

infector.register({ foo: { type: Foo, isSingleton: true } });
  • value: the registered value will be returned:
infector.register({ foo: { value: 'Foo' } });

Get modules

Just do:




A working example is available in the examples/browser dir. To view:

$ open examples/browser/index.html


A working example is available in the examples/node dir. To run:

$ node examples/node



To generate the docs, ensure the following is installed:

Install global dependencies:

$ npm install -g grunt-cli istanbul


$ git clone

Change to project dir, then install local dependencies:

$ cd infector && npm install

Grunt Tasks


To generate documentation via Docker:

$ grunt docs

Outputs to _docs. Open _docs/ in a browser to view.

Unit Tests

To run the unit tests via Mocha:

$ grunt test

Code Coverage

To run the unit tests and generate an HTML code coverage report via Istanbul:

$ grunt cover

Outputs to _coverage. Open _coverage/index.html in a browser to view.


To run JSHint over the required files:

$ grunt jshint


The start task will execute certain tasks, then will watch the relevant files for changes, running appropriate tasks when required:

$ grunt start

To find out more about the various Grunt tasks, see Gruntfile.js.


npm loves you