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 🙏

© 2025 – Pkg Stats / Ryan Hefner

inject-it

v1.2.0

Published

A really simple Dependency Injector library

Downloads

12

Readme

Inject It

Inject it provides a simple and easy way to handle your Node.js application level dependencies.

Installation

npm install -S inject-it

Configuring the injector

The inject-it porpuse is to work like a simple flat map of your application dependencies, so, imagine that it's just a hash with extra convenience, where you define your dependencies by name. While the name of the dependencies are all strings, the values of dependencies can be whatever you want, instances, classes...

What we recommend is for you to have a file just for your injector configuration, so you can easly reuse it over your application, here is a sample one:

config/injector.js

var injector = require('inject-it')()

injector.value('injector', injector); // I found that having the injector itself as a dependency is very helpful

injector.value('User', require('../app/models/user'));
injector.value('SomeLib', require('../lib/some_lib'));

module.exports = injector;

Using the injector

Once you have your injector configured, it's time to use it, let's start with the simplest way of reading dependencies, that's by using the get method:

var injector = require('./config/injector');

function doSomething() {
  var User = injector.get('User');

  var user = new User();
  user.sayHello();
}

doSomething();

Simple enough, right? And a notice, if you try to get a dependency that is not defined, an error with be raised.

The previous version works, but there is a more convenient way, that is to use the function argument names in order to fetch the dependencies, see the next example:

var injector = require('./config/injector');

function doSomething(User) {
  var user = new User();
  user.sayHello();
}

// this will read 'User' from the argument name and inject it automatically
injector.call(doSomething);

The cool thing about this is that you can just list your dependencies on the parameters (without even caring about the order) and have them propertly injected at runtime.

If you need to construct an object (calling new instead of simple calling the function) you can use the construct method:

var injector = require('./config/injector');

function MyClass(User) {
  this.User = User;
}

MyClass.prototype.sayHello = function() {
  var user = new this.User();
  user.sayHello();
};

injector.construct(MyClass).sayHello();