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

@art-ws/di

v2.0.42

Published

Dependency injection for TypeScript

Downloads

95

Readme

@art-ws/di

Lightweight, TypeScript-first dependency injection inspired by Angular but framework agnostic. Works in Node ESM/moduleResolution: nodenext projects and supports scoped injectors via AsyncLocalStorage.

Installation

pnpm add @art-ws/di

Concepts

  • Injector: resolves and caches instances; can be nested.
  • InjectionToken<T>: opaque token for non-class providers, optionally with a factory.
  • Providers (Angular-like): useClass, useFactory, useValue, useExisting, deps, multi.
  • Modules: DIModuleDef with imports and providers for ergonomic composition.
  • Scopes: Injector.runScope wires a child injector to AsyncLocalStorage so inject() resolves within the active scope.

Quick start

import { Injector, InjectionToken, inject } from "@art-ws/di"

class Config {
	constructor(readonly baseUrl = "https://api.service") {}
}

class Api {
	constructor(private cfg: Config) {}
	fetch(path: string) {
		return `${this.cfg.baseUrl}${path}`
	}
}

const TOKEN_MESSAGE = new InjectionToken<string>("MESSAGE", () => "hello")

const injector = new Injector()
Injector.root = injector

injector.addProviders(
	Config,
	Api,
	{ provide: TOKEN_MESSAGE, useFactory: () => "hi there" }
)

const api = inject(Api)
console.log(api.fetch("/ping")) // https://api.service/ping
console.log(inject(TOKEN_MESSAGE)) // hi there

Using modules

import type { DIModuleDef } from "@art-ws/di"
import { Injector, inject } from "@art-ws/di"

class Logger { log(msg: string) { console.log(msg) } }
class Service { constructor(private logger: Logger) {} run() { this.logger.log("ok") } }

const CoreModule: DIModuleDef = {
	name: "CoreModule",
	providers: [Logger, Service],
}

const injector = new Injector()
Injector.root = injector
injector.addModule(CoreModule)

inject(Service).run()

Scoped work (AsyncLocalStorage)

import { Injector, inject } from "@art-ws/di"

const root = new Injector()
Injector.root = root

await root.runScope(async () => {
	// child scope injector is active inside this async context
	const scoped = inject(Injector)
	console.log(scoped === root) // false
})

Utility helpers

  • runScopedTask({ task, deps, providers }): convenience to resolve deps and dispose scoped instances.
  • disposeInstances(instances): calls dispose() on scoped instances; errors are collected.
  • getModuleName(module): best-effort name for diagnostics.

Provider shapes (reference)

// Class provider
injector.addProviders(MyClass)

// Value
injector.addProviders({ token: TOKEN, factory: () => value })

// Angular-like
injector.addProviders({
	provide: TOKEN,
	useFactory: (a, b) => create(a, b),
	deps: [DepA, DepB],
})

injector.addProviders({ provide: TOKEN, useValue: 42 })
injector.addProviders({ provide: TOKEN, useExisting: OtherToken })
injector.addProviders({ provide: TOKEN, useClass: Impl, deps: [DepA] })

TypeScript notes

  • Published as ESM with type definitions; import with explicit .js paths in NodeNext if using path-mapped sources.
  • InjectionToken<T> can carry a factory used when no other provider exists.
  • DepsFor<C> enforces constructor arity when declaring useClass deps.

Testing

The project uses vitest. To run tests locally:

pnpm test

License

UNLICENSED (see package.json).