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

@eserstack/di

v5.1.0

Published

Readme

⚙️ @eserstack/di

Note: JSR is frozen at 4.5.x. From 5.0.0, @eserstack/di is published on npm under the same name (pnpm add @eserstack/di); the JSR versions stay available but get no updates.

eserstack Foundation — eser/stack on GitHub Install: pnpm add @eserstack/di

@eserstack/di is a crucial component of eserstack that provides simple and efficient dependency injection solutions.

🚀 Getting Started with Dependency Injection (DI)

Dependency injection is a software design pattern that enables the removal of hard-coded dependencies from the code, making it possible to change them whenever needed.

By promoting loose coupling, it enhances the testability and maintainability of the overall codebase structure.

Dependency Injection Container (DIC)

A dependency injection container is a software component responsible for the instantiation and distribution of services or objects in your code. It simplifies the management of dependencies by serving as a central registry for all services.

🤔 What @eserstack/di offers?

@eserstack/di helps you handle these dependencies by providing a structured, scalable and proper way to manage dependencies in your application. While its dependency injection container is focused on simplicity and efficiency, it also provides a set of decorators to make managing your application's services and dependencies even easier.

🛠 Usage

Here you'll find a list of features provided by @eserstack/di along with brief descriptions and usage examples.

Registering services

Start by creating a registry and registering your services using the set, setLazy, setScoped and setTransient methods.

import * as di from "@eserstack/di";

// Register the mailService as a singleton service
di.registry.set("mailService", new MailService());

// Register the notifyService as a singleton service
di.registry.set("notifyService", new PushNotificationService());

// Register the dbConnection as a transient service,
// which is created anew each time it is called
di.registry.setTransient("dbConnection", () => new DatabaseConnection());

// Register the userList as a lazy-loaded service,
// which is created when first called
di.registry.setLazy("userList", (container) =>
  container.get("dbConnection").query("SELECT * FROM users"),
);

Alternatively, you can chain the registration methods:

import * as di from "@eserstack/di";

di.registry
  .set("mailService", new MailService())
  .set("notifyService", new PushNotificationService())
  .setTransient("dbConnection", () => new DatabaseConnection())
  .setLazy("userList", (container) =>
    container.get("dbConnection").query("SELECT * FROM users"),
  );

Retrieving services

Once you have the services container, you may retrieve your services using the get, getMany, invoke methods. Even better, you can use the di template literal tag to retrieve services.

import * as di from "@eserstack/di";

// Retrieve registered services
const dns = di.di`mailService`;
const mns = di.di`notifyService`;
const db = di.di`dbConnection`;
const users = di.di`userList`;

Alternatively, retrieve multiple services at once:

import * as di from "@eserstack/di";

const [dns, mns, db, users] = di.services.getMany(
  "mailService",
  "notifyService",
  "dbConnection",
  "userList",
);

You can directly call your functions using the registered services as parameters:

import * as di from "@eserstack/di";

di.registry.set("serviceA", () => console.log("Service A"));
di.registry.set("serviceB", () => console.log("Service B"));

function myFunction(serviceA, serviceB) {
  serviceA();
  serviceB();
}

di.invoke(myFunction); // This will log "Service A" and then "Service B"

Template Strings

You can use the di template literal tag to easily retrieve services using template strings. This provides a more readable and concise way to access your dependencies.

import * as di from "@eserstack/di";

di.registry.set("serviceA", () => console.log("Service A"));
di.registry.set("serviceB", () => console.log("Service B"));

di.di`first: ${"serviceA"} second: ${"serviceB"}`; // This will log "first: Service A second: Service B"

Decorators

import * as di from "@eserstack/di";

@di.injectable()
class PrinterClass {
  print() {
    console.log("testing");
  }
}

const test = di.di`PrinterClass`;
test.print(); // outputs "testing"

The @ form needs a runtime or build step that transforms decorators, such as TypeScript's compiler or a bundler plugin. Node 26 strips types without transforming decorators and has no decorator syntax, so under Node call the decorator as a function:

import * as di from "@eserstack/di";

class PrinterClass {
  print() {
    console.log("testing");
  }
}
di.injectable()(PrinterClass);

📕 API Reference

The following is a list of all available methods and their descriptions.

Registry

set(key: string | symbol | object, value: any): Registry
Registers a singleton service with the specified key.

setLazy(key: string | symbol | object, factory: () => any): Registry
Registers a lazy-loaded service with the specified key. The value is created when first called.

setScoped(key: string | symbol | object, factory: () => any): Registry
Registers a scoped service with the specified key. The value is created once per scope.

setTransient(key: string | symbol | object, factory: () => any): Registry
Registers a transient service with the specified key. The value is created anew each time it is called.

build(): Container
Builds a dependency injection container with the registered services.

Container

get(key: string | symbol | object, defaultValue?: any): any
Returns the corresponding service to the specified key.

getMany(...keys: (string | symbol | object)[]): any[]
Returns the corresponding services to the specified keys in an array.

createScope(): Container
Initiates a new scope. Each new scope inherits all services from the root scope except those marked as 'scoped'. Scoped services are instantiated just once per scope. When a scope is closed (or disposed), all its scoped services lose their references as if they were never instantiated.

Upgrading from 4.x

These changes since 4.5.2 need a change in code, commands or files that use this package. The changelog lists the changes in every package.

  • The default export (the global di factory) is removed from the package root and from @eserstack/di/services. Use the namespace import:

    // Before
    import di from "@eserstack/di";
    // After
    import * as di from "@eserstack/di";
    
    const db = di.di`dbConnection`;

🔗 For further details, visit the eserstack repository.