Standardize and centralize the way objects are constructed in your application.
Inspired by the battle-tested Symfony DI container, built for Node.js & TypeScript.
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.
| π 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 |
npm install --save node-dependency-injectionRegister 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')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()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')Use NDI seamlessly with Express β retrieve the container directly from any request:
npm install --save node-dependency-injection-express-middlewareimport NDIMiddleware from 'node-dependency-injection-express-middleware'
import express from 'express'
const app = express()
app.use(new NDIMiddleware({ serviceFilePath: 'services.yaml' }).middleware())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.mailerThe full documentation lives in the project wiki, including guides on:
- Getting Started
- Autowire
- Configuration Files
- Compiler Passes
- Tagging Services
- Factory
- Lazy Services
- Decorators
- And much more...
Contributions are welcome! Please read the contribution guide before submitting a pull request.
Inspired by the Symfony Dependency Injection component β a special thanks to the Symfony team for their outstanding work.
MIT License Β Β·Β Made with β€οΈ by @zazoomauro
