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

nodejs-ioc

v0.1.3

Published

Simple ioc container for managing dependency injection in nodejs

Downloads

9

Readme

Nodejs-ioc

About

Nodejs-ioc is an inversion of control (IoC) container for JavaScript apps.

An IoC container uses a class constructor to identify and inject its dependencies.

Nodejs-ioc allows JavaScript developers to write code that adheres to the SOLID principles.

Installation

You can get the latest release using npm:

$ npm install nodejs-ioc --save

:warning: Important! Nodejs-ioc requires JavaScript ES6 

The Basics

Let’s take a look at the basic usage and APIs of Nodejs-ioc with JavaScript:

Step 1: Declare your types

const {Type} = require("nodejs-ioc");

const TYPES = {
    AUTH_SERVICE : Type.for('AUTH_SERVICE'),
    USER_SERVICE : Type.for('USER_SERVICE'),
    CONFIG : Type.for('CONFIG'),
    TEST_SERVICE : Type.for('TEST_SERVICE'),
};

Note: It is recommended to use Types but Nodejs-ioc also support the usage of string literals.

Step 2: Create and configure a Container

const {Container, inject} = require("nodejs-ioc");

class UserService {
}

class AuthService {
    constructor(){
        this.config = inject(TYPES.CONFIG);
        console.log('AuthService generated');
    }
}

const config = {
    conf:'conf'
};

const container = new Container();
container.bind(TYPES.USER_SERVICE).to(UserService);

module.exports = { container }

bind options

Binds to a singleton class

container.bind(TYPES.AUTH_SERVICE).toSingleton(AuthService);

Binds to a constant value

container.bind(TYPES.CONFIG).toConstantValue(config);

Step 3: Resolve dependencies

You can use the method get from the container class to resolve a dependency.

const authService = container.get(TYPES.AUTH_SERVICE);

expect(authService.config.conf).eql("conf"); // true

As we can see the config was successfully resolved and injected into AuthService.

inject

There are two important things to remember when using inject:

  • It must be used inside a constructor
  • The requested dependencies will be injected after the class instance is created — i.e., not inside the constructor (the inject keyword only marks the class members as flags and doesn't injects the dependency immediately).

For catching the dependencies' injection we can use onDependenciesInjected method

class AuthService {
    constructor(){
        this.config = inject(TYPES.CONFIG);
        //config is not injected yet
        console.log('AuthService generated');
    }

    onDependenciesInjected(){
        console.log('AuthService dependencies were injected', this.config);
    }

}

Injecting a Provider (asynchronous Factory)

Binds an abstraction to a Provider. A provider is an asynchronous factory, this is useful when dealing with asynchronous I/O operations.

container.bind(TYPES.TEST_SERVICE).toProvider((context) => {
    return (args) => {
        console.log(args);
        return new Promise((resolve) => {
            let userService = context.container.get(TYPES.USER_SERVICE);
            resolve(userService);
        });
    }

});

const testServiceProvider = container.get(TYPES.TEST_SERVICE);
testServiceProvider("hello").then((userService)=>{
    console.log(userService);
});

Lazy inject

Using lazyInject allows the container to inject dependencies into objects that are not bound to it. For example, when using a service in an Express middleware that exists inside a container.

lazyInject works differently from the inject method provided above, since it injects the dependency immediately (uses the get method)

lazyInject is linked to a specific container, hence needs to be exported after instantiating the container.

const lazyInject = container.lazyInject;

class HttpRequest{

    constructor(){
        this.authService = lazyInject(TYPES.AUTH_SERVICE);
        //authService is now injected
        console.log('HttpRequest generated', this.authService)
    }
}

const httpRequest = new HttpRequest();