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

@northern/di

v2.0.2

Published

A simple Dependency Injection container for JavaScript

Downloads

18

Readme

DI

A simple Dependency Injection container for JavaScript & TypeScript.

Install

Either use npm:

npm install @northern/di

or Yarn:

yarn add @northern/di

Introduction

To use DI in your application simply import (or require) the Container and create a Container instance:

import Container from '@northern/di'

const container = new Container()

With the newly instantiated container, you can start registering services. There are two ways you can register a service, either use the service method or you can use the factory method.

Registering a service

With the service method a new service provider can be registered. The service method takes three parameters:

Container.service(name, provider[, lazy = true, overwrite = false])

The name is a string with the "name" of the service, e.g. logger. The provider is a function that, when called, "returns" the service instance. The lazy parameter specifies whether the service is "lazy" or not, more on that later and the overwrite parameter allows you to re-register a service if it already exists in the service registry of the container (handy during integration tests when you want to swap a service with a mock).

A "service" instance is only ever created once (as opposed to a factory, which returns a new instance of the service each time).

Let's look at a simple service registration example:

class Logger {
  info(message) {
    console.info(message)
  }
}

container.service('logger', container => {
  return new Logger()
})

It's that simple. We can now get the "logger" service by using the get method on the container:

const logger = container.get('logger')

logger.info("Hello DI")

Since the Container instance is passed into the service provider, it is possible to "wire" multiple services together (i.e. create service dependencies by having one service require another already registered service). A service provider can use already registered services and "inject" them into other services. E.g. if we register a service then we can pass an instance of the logger service into that other service:

class PaymentService {
  constructor(logger) {
    this.logger = logger
  }

  processPayment(paymentObject) {
    this.logger.info("Starting payment process.")
  }
}

container.service('paymentService', container => {
  const logger = container.get('logger')

  const service = new PaymentService(logger)

  return service
})

const paymentService = container.get('paymentService')
paymentService.processPayment({ ... })

Lazy services

When a service is not "lazy" then the instance of that service will be created the moment the service is registered (i.e. when the service method is called). This is not usually desireable (and why lazy by default is true). E.g. if the dependency graph is large and not all services are always used, i.e. usage depends of the code path of the application, it might be better to instantiate a service on a need by need basis. This what a "lazy" service does; the instance is created the first time the service is requested rather than when the service was registered.

Registering a factory

With the factory method a new factory provider can be created. A factory will always return a new instance each time it is requested from the container. The factory registration is the same as that of a service except that a factory cannot be lazy (i.e. its always lazy).

The factory method only has three parameters:

Container.factory(name, provider[, overwrite = false])

That's it. Have fun wiring!