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

ioc-node

v2.3.2

Published

IoC container for Node

Downloads

49

Readme

Zero dependency IOC container for Node

Installation

npm install ioc-node

Instantiate

// index.js

global.ioc = require('ioc-node')(__dirname)

Usage

Imagine the following class

class UserService {
    constructor(database) {
        this.database = database
    }
}

You can inject dependencies using

ioc.bind('userService', () => new UserService(new Database))

and later make use of the binding with

ioc.use('userService').create({ id: 1})

If you don't want to create a new instance every time you use ioc.use, create the binding with ioc.singleton instead of ioc.bind.

ioc.singleton('userService', () => new UserService(new Database))

ioc.use('userService').create({ id: 1})
ioc.use('userService').create({ id: 2}) // uses same instance

Faking

You can easily fake bindings in your tests!

class TestableDatabase {
    insert() { return true }
}

ioc.fake('userService', () => new UserService(new TestableDatabase))

const userService = ioc.use('userService')
// assert...

ioc.restore('userService') // remove the fake again

Global Require

The use method can also require files globally from any point in the app.

const User = ioc.use('app/models/User')

Automatic Injection

It might be cumbersome to always add bindings, that's why ioc-node also supports automatic injection. Since there are no static types we have to workaround a little to get this working.

Instead of registering bindings in the service provider using ioc.bind('userService', () => new UserService(new Database)), you can link to the dependency directly in the class

// App/Services/UserService
class UserService {
    static get inject() {
        return ['path/to/Database']
    }

    constructor(database) {
        this.database = database
    }
}

And instead of newing up the class manually, we do

const userService = ioc.make('App/Services/UserService') // will use ioc.use to resolve dependency

or

const UserService = ioc.use('App/Services/UserService')
const userService = ioc.make(UserService)

You can also pass additional arguments to the constructor

const userService = ioc.make('App/Services/UserService', 1, 2, 3, 4)

Faking Automatic Injection

In the above case, you can still use ioc.fake to provide a fake database or a fake userService since ioc.make('App/Services/UserService') uses ioc.use under the hood. In addition, every auto injected dependencies (e.g. "path/to/Database") also gets resolved using ioc.use.

Alternatively you can just new up an instance of the class manually.

const UserService = ioc.use('App/Services/UserService')
class TestableDatabase {
    insert() { return true }
}

const userService = new UserService(new TestableDatabase))

Aliases

When using ioc.bind or ioc.singleton we can access the bindings using the key we provide. Global requires and automatic injections don't provide that flexibility out of the box. Say the file Cache.js is inside App/Utils/. You have to require it using ioc.use('App/Utils/Cache'), while you might actually want to do ioc.use('Cache').

For this you can use aliases.

// in service provider
ioc.alias('Cache', 'App/Utils/Cache')

// anywhere
const cache = ioc.make('Cache')
//or
const Cache = ioc.use('Cache')

We can also use the alias for automatic injections.

class UserService {
    static get inject() {
        return ['Cache']
    }

    // ...
}

How ioc.use resolves dependencies

  1. Look in fakes (ioc.fake)
  2. Look in container (ioc.bind / ioc.singleton)
  3. Look in aliases (ioc.alias)
    1. Repeat process with resolved name
  4. Native require from root of the project

Creating Providers

If you extract code into separate npm modules, you can create a provider that can be easily consumed by the ioc container.

Say you have the following module

class StringTransformer {
    toUpperCase(value) {
        return value.toUpperCase()
    }
}

module.exports = StringTransformer

In the same module, you can create a provider like this

class StringTransformerProvider {
    register(ioc, namespace) {
        ioc.singleton(namespace, () => new StringTransformer)
    }
}

module.exports = StringTransformerProvider

and inside the service provider of your app, you can consume this provider

ioc.consume('App/StringTransformer', StringTransformerProvider)