Skip to content

DI BagOrdinary services. Checked composition. Explicit ownership.

A missing dependency is a type error. One feature is one directory that type-checks alone. Resources have one owner.

Dependency injection for TypeScript where the compiler checks the wiring. A module keeps a feature's services private behind named exports; a container creates services on first use and releases what it owns when closed.

npmCIRuntime dependencies: 0License: MIT

Documentation · npm · Quickstart · Modules as units of work · Comparison · Tutorial · API reference

Using a coding agent? The rules, the API card, the recipes, and every error message ship in the package. Paste this into your agent:

text
If di-bag is installed: read node_modules/di-bag/AGENTS.md first and follow its rules.
Use node_modules/di-bag/docs/agent/api-card.md for a call's signature and example,
node_modules/di-bag/docs/agent/recipes.md for a task, and
node_modules/di-bag/docs/agent/errors.md for a compiler or runtime message.

If di-bag is not installed: evaluate it from these pages before deciding.
- https://dany-fedorov.github.io/di-bag/ : what it does, the compiler check, a module in one screen
- https://dany-fedorov.github.io/di-bag/llms-full.txt : AGENTS.md, the API card, recipes, and errors in one file
- https://dany-fedorov.github.io/di-bag/guides/examples-modularity.html : the module layout and three runnable programs
- https://dany-fedorov.github.io/di-bag/guides/comparison.html : how it differs from Awilix, InversifyJS, TSyringe, Typed Inject, Effect, and NestJS
To install: npm install di-bag. It needs TypeScript 6.0.3 or later with "strict": true and has no
runtime dependencies. Node, Bun, and Deno need nothing else; in browsers and workers, pass
factoryReturnKind to each factory. After installing, read node_modules/di-bag/AGENTS.md.

Why DI Bag? ​

  • A missing dependency is a type error, not a crash on the first request. Delete a factory and tsc names the missing service. Change a contract and tsc rejects the consumer that no longer fits. See it.
  • One feature is one directory that type-checks alone. A module exports only the services it names; everything else stays private, so modules written separately can reuse the same internal names without colliding. A one-statement check file and a per-module tsconfig.json verify the module without the rest of the application. See a module.
  • Resources have one owner. Attach a disposer to a factory and close() runs it, dependents first. A child container per request releases what the request acquired. See ownership.

No decorators, no reflection metadata, no base classes, and zero runtime dependencies. Runs on Node, Bun, Deno, browsers, and workers.

Install ​

sh
npm install di-bag

TypeScript 6.0.3 or later with strict enabled. Browsers and workers need one extra option per factory; see runtime support. Coming from 0.4? See upgrading.

Quickstart ​

A factory creates a service. The type of a factory's one object parameter declares what it depends on. A container calls factories on first use:

ts
import { DiBag } from 'di-bag';

const app = DiBag.createBuilder()
  .withServices({
    config: () => ({ greeting: 'Hello' }),
    greeter: ({ config }: { config: { greeting: string } }) => ({
      greet(name: string) {
        return `${config.greeting}, ${name}!`;
      },
    }),
  })
  .buildContainer();

const greeter = app.resolve('greeter');
console.log(greeter.greet('Ada')); // Hello, Ada!
await app.close();

resolve('greeter') creates the greeter and its config the first time, and returns the same greeter afterwards. TypeScript knows that greeter has a greet(name: string): string method, with no cast and no token.

What the compiler catches ​

Remove the config factory. buildContainer() no longer type-checks, and the message names the missing service:

ts
import { DiBag } from 'di-bag';

const incomplete = DiBag.createBuilder().withServices({
  greeter: ({ config }: { config: { greeting: string } }) => ({
    greet(name: string) {
      return `${config.greeting}, ${name}!`;
    },
  }),
});

// @ts-expect-error required services are missing: config
incomplete.buildContainer();
text
required services are missing: config; see https://dany-fedorov.github.io/di-bag/agent/errors.html#missing-service

Change greeting to a number and, with default settings, the message is provided service does not satisfy its consumer dependency. With "noErrorTruncation": true in tsconfig.json, the details name the consumer, the dependency, the expected type, and the provided type. The error index lists every message with its cause and fix. The directive above is not decoration: this repository compiles the block in CI, and an unused @ts-expect-error fails the build.

A module in one screen ​

A DI Bag module seals a service graph with the keys it exports. The host supplies the module's requirements, and a DI Bag container owns the services created at runtime. The file suffixes mark each role. See the full layout and the per-module check in AGENTS.md.

ts
// src/features/greeting/contract.ts
export type Greeter = { greet(name: string): string };
export type GreetingConfig = { greeting: string };
ts
// src/features/greeting/greeting.di-bag-module.ts
import { DiBag } from 'di-bag';
import type { Greeter, GreetingConfig } from './contract.js';

export const greetingDiBagModule = DiBag.createBuilder()
  .withServices({
    template: ({ config }: { config: GreetingConfig }) => config.greeting,
    greeter: ({ template }: { template: string }): Greeter => ({
      greet: name => `${template}, ${name}!`,
    }),
  })
  .buildModule({ exportedServiceKeys: ['greeter'], moduleLabel: 'greeting' });
ts
// src/app.di-bag-container.ts
import { DiBag } from 'di-bag';
import type { GreetingConfig } from './features/greeting/contract.js';
import { greetingDiBagModule } from './features/greeting/greeting.di-bag-module.js';

export function createAppDiBagContainer() {
  return DiBag.createBuilder()
    .withInstalledModules([greetingDiBagModule])
    .withServices({ config: (): GreetingConfig => ({ greeting: 'Hello' }) })
    .buildContainer();
}
ts
// src/main.ts
import { createAppDiBagContainer } from './app.di-bag-container.js';

const appDiBagContainer = createAppDiBagContainer();
try {
  console.log(appDiBagContainer.resolve('greeter').greet('Ada')); // Hello, Ada!
} finally {
  await appDiBagContainer.close();
}

appDiBagContainer.resolve('template') is a type error because template is private. Leave out the config factory and the host fails with required services are missing: config. Another module may also call a private service template; the names never meet. Modules as units of work explains the layout, the check file, and the merge check.

Swap a dependency for a test ​

Use createIndependentContainer() for a test with a replacement dependency. This example uses the graph from the quickstart:

ts
import { DiBag } from 'di-bag';

const app = DiBag.createBuilder().withServices({
  config: () => ({ greeting: 'Hello' }),
  greeter: ({ config }: { config: { greeting: string } }) =>
    ({ greet: (name: string) => `${config.greeting}, ${name}!` }),
}).buildContainer();

const testApp = app.createIndependentContainer(['config'], {
  config: () => ({ greeting: 'Hi' }),
});

try {
  console.log(testApp.resolve('greeter').greet('Ada')); // Hi, Ada!
  console.log(app.resolve('greeter').greet('Ada')); // Hello, Ada!
} finally {
  await testApp.close();
  await app.close();
}

The replacement must satisfy the original service contract. Each independent container owns its own acquisitions and disposal. Close it separately. Factories can still return shared objects captured outside the container.

Work with async services ​

An async factory provides a promise. Declare that promise in any dependent factory and await it where you need the value:

ts
import { DiBag } from 'di-bag';

const app = DiBag.createBuilder()
  .withServices({
    greeting: async () => 'Hello',
    message: async ({ greeting }: { greeting: Promise<string> }) =>
      `${await greeting}, Ada!`,
  })
  .buildContainer();

console.log(await app.resolve('message')); // Hello, Ada!
await app.close();

Within one container, repeated resolutions share the same pending promise. Synchronous factories keep returning ordinary values. See async behavior for details.

Give resources a clear owner ​

Use DiBag.providerWithDisposal to tell the container how to release a factory's result:

ts
import { DiBag } from 'di-bag';

const resources = DiBag.createBuilder()
  .withServices({
    cache: DiBag.providerWithDisposal({
      provider: () => new Map<string, string>(),
      disposeService: cache => cache.clear(),
    }),
  })
  .buildContainer();

try {
  resources.resolve('cache').set('answer', '42');
} finally {
  await resources.close();
}

The same pattern works for connections, clients, and subscriptions. Disposal can be asynchronous. Dependents close before their dependencies, and resources that were never created need no disposal. Ordinary factories return borrowed values. Having a close() method alone does not transfer ownership to the container.

Child and independent containers ​

OperationWhat it createsWho closes it?
container.createChildContainer()A tracked child with its own scoped servicesClose it when its work ends. The parent closes live children.
container.createChildContainer(keys, providers)A tracked child with selected scoped or transient services replacedClose it when its work ends. The parent closes live children.
container.createIndependentContainer()A separate container with fresh instancesThe caller closes it separately.
container.createIndependentContainer(keys, providers)A separate container with selected dependencies replacedThe caller closes it separately.

An unmarked provider is scoped. Choose singleton explicitly when child containers should share it.

For request handling, checked test replacements, and loading dynamic features, see the server guide and Integration patterns.

The tutorial also covers typed tokens, class and function adapters, optional and lazy dependencies, collections, service readiness, metadata, observers, and plugin validation.

Modules as units of work ​

A module is the unit of work one person or one coding agent can own: a directory with a small exported contract, private services, and its own tests. Composition is checked when the modules meet, so several can be developed in parallel and merged with confidence.

  • A boundary an owner can hold. buildModule({ exportedServiceKeys }) exports only the named services. Private services and their types stay inside, and two modules can use the same private names.
  • Verification without the whole application. A module declares what its host must supply. A one-statement check file installs the module with typed fixtures and calls verifyGraphAtCompileTime(); tests use createIndependentContainer(keys, providers) for checked replacements.
  • Checks at merge time. Installing every module into one builder is where independently developed work meets. A missing requirement or a contract that no longer matches its consumers fails at buildContainer() or verifyGraphAtCompileTime(); di-bag-graph exports the declared edges and cycles for review.

If two modules require the same name for different contracts, rename each requirement at the install site with withRenamedRequirement({ currentRequirementKey, newRequirementKey }), and colliding exports with withRenamedExport({ currentExportKey, newExportKey }).

The directory layout is the map: one directory per module, the contract first. The modules guide shows the layout and three runnable programs. LLM harnesses and agent graphs are one application of the pattern; the agent harness guide is a complete example.

How it compares ​

DI Bag's appeal is the combination of object-parameter factories, checks across the declared graph, and explicit resource ownership. Decorator-free composition, async factories, and TypeScript support are also available in other libraries.

AlternativeReasons to choose itDI Bag's different emphasis
Manual dependency injectionDirect function calls may be all a small application needs. TypeScript checks their arguments.Adds lazy caching, graph-wide composition checks, child containers, and coordinated disposal.
AwilixFunction and class registration, inferred cradle types, lifetime options, and runtime strict checks.Checks declared factory requirements against the registrations at compile time.
InversifyJS / TSyringeToken and class-oriented containers; Inversify also offers decorator-free factory bindings and awaited async resolution.Starts with object-parameter factories and immutable builders; checks accumulated graph contracts.
Typed InjectA close alternative with compile-time dependency checks, explicit dependency tuples, child injectors, and disposal.Adds object-parameter dependencies, forward references, private module exports, and selected service readiness with rollback.
Effect Context / LayerTyped requirements, scoped resources, and composition within Effect's broader async and error model.Keeps ordinary T and Promise<T> service values and explicit container ownership.
NestJS / Angular DITheir native containers connect directly to framework components, testing tools, and lifecycles.Provides standalone composition; applications supply the framework integration.

See the comparison guide for primary sources, differences in async and disposal behavior, and the limits of these comparisons. There is no verified performance ranking against these libraries.

Runtime support ​

The package has zero runtime dependencies and one entry point: di-bag. The root import configures native Promise detection on Node, Bun, and Deno. It also bundles for browsers and workers because it has no node: imports.

In browsers and workers, buildContainer() names any factory that needs an explicit return kind. Register synchronous factories with DiBag.createProvider(factory, { factoryReturnKind: 'sync-value' }) and native Promise factories with 'native-promise'. See portable mode.

Tradeoffs and limits ​

  • Agent context is still your responsibility. DI Bag does not choose module boundaries, manage an agent's context window, or replace behavioral tests.
  • Async dependencies are explicit. A factory returning Promise<T> exposes that promise. Consumers declare and await it themselves.
  • Disposal waits for your work by default. Cancellation is cooperative; a factory or disposer that never settles keeps close() pending. Pass close({ waitTimeoutMs, abortSignal }) to stop waiting. The rejection names the disposers still running, and disposal continues in the background.
  • Type safety follows the declared graph. Casts, unchecked JavaScript, and unknown plugins need appropriate runtime checks. Dependency cycles are detected at runtime, or before running by di-bag-graph.
  • Graph types have a compiler cost. One fluent expression is bounded by the compiler's recursion budget: classic TypeScript 6.0.3 accepts about 1,000 chained calls and overflows beyond that (about 950 for a bulk map followed by individual replacements); native 7.0.2 has no such ceiling. Keep an expression to 500 calls or fewer and use bulk registration, groups, or named modules beyond that. See the compiler evidence.
  • Framework integration belongs to the application. DI Bag provides the composition and ownership primitives; the host connects request, job, or UI lifecycles. The server guide and the React guide are recipes for both; the React recipe runs in CI, the server recipes compile where they import only di-bag and node:*.

Explore further ​

ResourceWhat you'll find
Complete tutorialLearn every public API through examples, from first composition to advanced ownership.
API referenceExact generated signatures, overloads, type parameters, and API inventories.
Server guideNode HTTP, Express, Fastify, Bun, and Deno: shared services, request containers, service readiness, and shutdown.
React guideBrowser applications: one app runtime at bootstrap, project runtimes owned from effects, Strict Mode, cancellation, bounded teardown, and useSyncExternalStore.
Modules as units of workThe recommended module layout, separately owned features, isolated tests, and contributed tools.
Agent docsRules, module layout, and check commands for coding agents, with recipes and errors. Shipped in the package.
Agent harnesses and graphsOne worked application: model and tool modules, metadata inspection, and node tests with typed fixtures.
Static dependency graphExport every builder chain, declared edge, and cycle to JSON with di-bag-graph for merge review and CI.
Runnable examplesOne program per topic: modules, tokens, composition, collections, plugins, observers, child containers, and provider metadata.
Integration patternsTested recipes for request ownership, substitutions, and dynamic features.
Comparison with alternativesWhen DI Bag or another approach may be a better fit, with primary sources.
Development and verificationFull checks, portable runtime testing, compiler scale, and performance evidence.
Documentation mapEvery guide, the generated reference, and the contributor documents.

Working on DI Bag ​

After npm ci, run the main checks with Node and Bun installed:

sh
npm run platform:pin
npm run check
npm run check:native

Run an example with bun run examples/composition.ts. The development guide covers the remaining compiler, platform, and packaging checks and their tool requirements.

Upgrading ​

The API is pre-1.0 and includes breaking changes. Run the 0.4-to-0.5 codemod before upgrading, while the 0.4 types are still installed, then read the 0.5 migration guide and the changelog:

sh
npx di-bag-codemod --project tsconfig.json --write

License ​

MIT © Dany Fedorov

Ordinary services. Checked composition. Explicit ownership.