README.md

May 5, 2026 ยท View on GitHub

Node Dependency Injection

Node Dependency Injection

Standardize and centralize the way objects are constructed in your application.
Inspired by the battle-tested Symfony DI container, built for Node.js & TypeScript.

Npm Version Build Status Code Coverage Npm Downloads Known Vulnerabilities License


Why Node Dependency Injection?

Managing dependencies manually leads to tightly coupled, hard-to-test code. Node Dependency Injection gives you a powerful, flexible IoC container that wires your application together โ€” keeping your classes clean, your tests simple, and your architecture solid.


โœจ Features

๐Ÿ”„ Autowire โ€” zero-config DI for TypeScript๐Ÿ“ Config files โ€” YAML, JSON or JS
๐Ÿญ Factory pattern โ€” flexible service creation๐Ÿท๏ธ Service tagging โ€” group & inject by tag
๐Ÿ’ค Lazy services โ€” instantiate only when needed๐ŸŽจ Decorators โ€” wrap services transparently
โšก Compiler passes โ€” transform the container at build time๐Ÿ”’ Private services โ€” encapsulate internals
๐ŸŒณ Parent / Abstract services โ€” share config via inheritance๐Ÿงฉ Non-shared services โ€” new instance per call
๐ŸŒฟ Environment parameters โ€” %env(VAR)% support๐Ÿ—‘๏ธ Deprecation warnings โ€” mark services as deprecated
๐Ÿ“ฆ Express middleware โ€” first-class web framework support๐Ÿ–ฅ๏ธ CLI โ€” inspect & validate your container

๐Ÿš€ Installation

npm install --save node-dependency-injection

๐Ÿ Quick Start

Register services and wire them together in seconds:

import { ContainerBuilder } from 'node-dependency-injection'
import Mailer from './services/Mailer'
import ExampleService from './services/ExampleService'

const container = new ContainerBuilder()

container.register('service.example', ExampleService)
container.register('service.mailer', Mailer).addArgument('service.example')

await container.compile()

const mailer = container.get('service.mailer')

๐Ÿ”„ Autowire (TypeScript)

Zero-configuration dependency injection โ€” NDI reads your TypeScript type annotations and wires everything automatically:

import { ContainerBuilder, Autowire } from 'node-dependency-injection'

const container = new ContainerBuilder(false, '/path/to/src')
const autowire = new Autowire(container)
await autowire.process()
await container.compile()

// Retrieve by class โ€” no string IDs needed
import SomeService from '@src/service/SomeService'
const service = container.get(SomeService)

Production tip: dump the autowired config to a YAML file and load it directly in prod โ€” no TypeScript scanning overhead.

if (process.env.NODE_ENV === 'development') {
  const autowire = new Autowire(container)
  autowire.serviceFile = new ServiceFile('/dist/services.yaml')
  await autowire.process()
} else {
  const loader = new YamlFileLoader(container)
  await loader.load('/dist/services.yaml')
}
await container.compile()

๐Ÿ“ Configuration Files

Prefer declarative config? Use YAML, JSON or JS:

# services.yaml
services:
  service.example:
    class: 'services/ExampleService'

  service.mailer:
    class: 'services/Mailer'
    arguments: ['@service.example']
import { ContainerBuilder, YamlFileLoader } from 'node-dependency-injection'

const container = new ContainerBuilder()
const loader = new YamlFileLoader(container)
await loader.load('/path/to/services.yaml')
await container.compile()

const mailer = container.get('service.mailer')

๐Ÿ“ฆ Ecosystem

Express Middleware

Use NDI seamlessly with Express โ€” retrieve the container directly from any request:

npm install --save node-dependency-injection-express-middleware
import NDIMiddleware from 'node-dependency-injection-express-middleware'
import express from 'express'

const app = express()
app.use(new NDIMiddleware({ serviceFilePath: 'services.yaml' }).middleware())

Express Middleware Documentation

CLI

Inspect and validate your container from the command line:

# Validate a config file
ndi config:check /path/to/services.yaml

# Inspect a specific service
ndi container:service /path/to/services.yaml service.mailer

๐Ÿ“– Documentation

The full documentation lives in the project wiki, including guides on:


๐Ÿค Contributing

Contributions are welcome! Please read the contribution guide before submitting a pull request.


๐Ÿ™ Credits

Inspired by the Symfony Dependency Injection component โ€” a special thanks to the Symfony team for their outstanding work.


MIT License ย ยทย  Made with โค๏ธ by @zazoomauro