Awilix is a TypeScript-written inversion-of-control container for Node.js and browsers. It wires modules together without annotation-heavy setup so application code stays decoupled from DI mechanics.
Overview
Awilix provides a dependency injection container for JavaScript and Node, emphasizing composable and testable applications. The typical flow is creating a container, registering modules, and resolving services when needed.
Lifetime management covers transient, scoped, and singleton registrations, with scoped lifetimes suited to per-request web work. Strict mode adds checks for lifetime mismatches and singleton registration mistakes. Injection modes include PROXY (default) and CLASSIC parameter matching; CLASSIC is noted as unsuitable for minified code.
Module auto-loading scans file globs to register classes and factories, with per-module local injections for configuration values. Resolvers support inline metadata, aliases, and disposal hooks; container.dispose clears caches and runs disposers, useful for tests and connection cleanup.
A universal module build enables browser use alongside Node. The API centers on createContainer, asClass, asFunction, asValue, loadModules, and scoped createScope operations.
Key features
- Transient, scoped, and singleton lifetimes
- PROXY and CLASSIC injection modes
- Automatic module loading via globs
- Per-module local injection values
- Scoped child containers
- Registration disposal on shutdown
- TypeScript utility types for cradles
- UMD build for browser usage
Use cases
- Wiring Node.js service layers
- Per-request scoped dependencies
- Test doubles via container overrides
- Auto-registering many service modules
- Cleaning up connections after tests