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

@moln/dependency-container

v1.0.6

Published

A lightweight dependency injection container for TypeScript/JavaScript for constructor injection.

Readme

Dependency-container

Build Status Coverage Status GitHub license npm

A lightweight dependency injection container for TypeScript/JavaScript for constructor injection.

Installation

Install by npm

npm install --save @moln/dependency-container

or install with yarn (this project is developed using yarn)

yarn add @moln/dependency-container

Modify your tsconfig.json to include the following settings

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

Add a polyfill for the Reflect API (examples below use reflect-metadata). You can use:

The Reflect polyfill import should only be added once, and before DI is used:

// main.ts
import "reflect-metadata";

// Your code here...

API

Decorators

injectable()

Class decorator factory that allows the class' dependencies to be injected at runtime.

usage
// foo.ts
import {injectable, createContainer} from "@moln/dependency-container";

@injectable()
export class Foo {
  constructor(private database: Database) {}
}

// some other file
const container = createContainer();

const instance = container.get(Foo);

inject()

Parameter decorator factory that allows for interface and other non-class information to be stored in the constructor's metadata.

usage
// foo.ts
import {injectable} from "@moln/dependency-container";

@injectable()
class Foo {
  constructor(@inject('bar') private bar: Bar) {}
}

class Bar {
  constructor(name: string) {}
}

// some other file
const container = createContainer();

container.register('bar', () => new Bar('abc'));

const instance = container.get(Foo);

DependencyContainer

InjectionToken

A token may be either a string, a symbol or a class constructor.

type InjectionToken<T = any> = constructor<T> | string | symbol;

Provider

Our container has the notion of a provider. A provider is registered with the DI container and provides the container the information needed to resolve an instance for a given token.

type FactoryFunction<T> = (
  container: DependencyContainerInterface,
  token: InjectionToken<T>
) => T;

interface Provider<T = any> {
  factory: FactoryFunction<T>;
  lifecycle: Lifecycle;
  instance?: T;
}

Registers

const container = new DependencyContainer();
container.register(Foo, () => new Foo()); // Register by factory
container.register(Foo, {factory: () => new Foo(), lifecycle: Lifecycle.SINGLETON}); // Full provider config

container.registerSingleton(Foo);
container.registerSingleton('MyFoo', Foo);

container.registerInstance(Bar, new Bar());
container.registerInstance('MyBar', new Bar());

Factories

reflectionFactory & reflectionFactoryFrom
const container = new DependencyContainer();

container.register(Foo, reflectionFactory);
container.register("MyBar", reflectionFactoryFrom(Bar));
aliasFactory
const container = new DependencyContainer();

container.register(Foo, () => new Foo);
container.register('aliasFoo', aliasFactory(Foo));
container.register('aliasFoo2', aliasFactory('aliasFoo'));

container.get(Foo) === container.get('aliasFoo')
ReflectionBasedAbstractFactory

Register (default) singleton reflection abstract factory.

const container = createContainer()

@injectable()
class Foo {
  constructor(private bar: Bar) {}
}

class Bar {
  constructor() {}
}

const instance = container.get(Foo)

Register transient reflection abstract factory.

const container = new DependencyContainer();
container.configure({
  abstractFactories: [[new ReflectionBasedAbstractFactory(), Lifecycle.TRANSIENT]],
});

@injectable()
class Foo {
  constructor(private bar: Bar) {}
}

class Bar {
  constructor() {}
}

const foo1 = container.get(Foo);
const foo2 = container.get(Foo);

foo1 !== foo2 // true

Middleware

When active the service. Middlewares will be call.

const container = new DependencyContainer();
const loggerMiddleware: ServiceMiddleware = (container, token, next) => {
 console.log('Start ' + (token.name || token))
 const instance = next();
 console.log('End ' + (token.name || token))
 return instance;
}

container.use(loggerMiddleware)

class Foo {}

container.registerSingleton(Foo)

const foo = container.get(Foo);
// console output:
// Start foo
// End foo

const foo = container.get(Foo); 
// Foo is singleton, never be call middleware.
matchMiddleware
const container = new DependencyContainer();
const loggerMiddleware: ServiceMiddleware = (container, token, next) => {
 console.log('Start ' + (token.name || token))
 const instance = next();
 console.log('End ' + (token.name || token))
 return instance;
}

container.use(matchMiddleware(Foo, [loggerMiddleware]))

class Foo {}
class Bar {}

container.registerSingleton(Foo)
container.registerSingleton(Bar)

const foo = container.get(Foo);
// console output:
// Start foo
// End foo

const bar = container.get(Bar); 
// Not matched, never be call `loggerMiddleware`.