Registry / web-framework / typedi

typedi

JSON →
library0.6.0jsnpmunverified

TypeDI is a dependency injection framework specifically designed for TypeScript and JavaScript applications. It enables the creation of loosely coupled, well-structured, and easily testable applications in both Node.js and browser environments. The current stable version is 0.10.0, with releases occurring periodically to introduce new features, improvements, and bug fixes, as indicated by the recent 0.9.x to 0.10.0 progression. Key differentiators include support for both property and constructor-based injection, management of singleton and transient services, and the ability to work with multiple DI containers within a single application. It heavily leverages TypeScript decorators and the `reflect-metadata` polyfill to achieve its injection capabilities, making proper configuration of `tsconfig.json` crucial for its operation.

npm install typedi
INSTALL
IMPORT
SIG · TYPEDI
T
typedi
web-frameworkjavascriptv0.6.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Container
✓ import { Container } from 'typedi';
✗ const Container = require('typedi').Container;
TypeDI is primarily designed for ESM consumption, especially with TypeScript decorators. CommonJS `require` is generally discouraged due to potential metadata issues and awkward syntax.
Service
✓ import { Service } from 'typedi';
✗ import Service from 'typedi';
`Service` is a named export, not a default export. Ensure correct destructuring.
'reflect-metadata'
✓ import 'reflect-metadata';
✗ require('reflect-metadata');
This import must be the very first line of your application's entry point to ensure the `Reflect` API is available globally before any decorators are processed.

Demonstrates basic setup with `reflect-metadata` and TypeDI's `@Service()` decorator for both constructor and property injection, including usage of environment variables.

import 'reflect-metadata'; import { Container, Service } from 'typedi'; // A service that will be injected @Service() class MailerService { private readonly apiKey: string; constructor() { this.apiKey = process.env.MAILER_API_KEY ?? 'default-api-key'; } sendMail(to: string, subject: string, body: string): void { console.log(`Sending email to ${to} with subject '${subject}' via API Key: ${this.apiKey}`); console.log(`Body: ${body}`); } } // A service that depends on MailerService @Service() class UserService { constructor(private mailer: MailerService) {} registerUser(email: string, username: string): void { console.log(`Registering user: ${username} (${email})`); this.mailer.sendMail(email, 'Welcome!', `Hello ${username}, welcome to our service!`); } } // Get an instance of UserService from the container, which will automatically inject MailerService const userService = Container.get(UserService); userService.registerUser('john.doe@example.com', 'JohnDoe'); // You can also get other services directly const mailerService = Container.get(MailerService); mailerService.sendMail('admin@example.com', 'System Alert', 'Server running fine!');
Debug
Known issues
breakingTypeDI versions prior to 0.6.0 had slightly different decorator application and API for registering services. While 0.10.0 is stable, always consult release notes for major version updates from older versions.
fix
Upgrade to the latest stable version and review the official documentation for API changes, especially around `@Service` and `Container` methods.
affects: <0.6.0
gotchaThe `reflect-metadata` package *must* be imported as the very first line of your application's entry file. Failing to do so will result in runtime errors related to missing metadata when decorators are processed.
fix
Add `import 'reflect-metadata';` to the absolute top of your main application file (e.g., `src/main.ts` or `app.ts`).
affects: >=0.1.0
gotchaTypeDI relies on TypeScript's experimental decorators and metadata emission. Your `tsconfig.json` must include `"emitDecoratorMetadata": true` and `"experimentalDecorators": true` under `compilerOptions`.
fix
Ensure the specified `compilerOptions` are present in your `tsconfig.json` file. Without them, decorators will not function as expected for DI.
affects: >=0.1.0
gotchaCircular dependencies can occur when Service A depends on Service B, and Service B also depends on Service A. TypeDI can sometimes resolve simple circular dependencies with constructor injection if not deeply nested, but complex cases can lead to runtime errors or `undefined` injections.
fix
Refactor your services to break circular dependencies, perhaps by introducing an interface, an event emitter, or restructuring responsibilities. Lazy injection patterns might also help in specific scenarios.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: Reflect.metadata is not a function
`reflect-metadata` was not imported, or not imported as the very first line of your application.
fix
Ensure `import 'reflect-metadata';` is the absolute first line in your main application file before any other imports.
Error: Service 'MyService' was not found in the container. Make sure you have decorated it with @Service() and registered it.
The class intended for injection either lacks the `@Service()` decorator or the container cannot resolve its dependencies due to a misconfiguration or circular dependency.
fix
Verify that `MyService` has `@Service()` applied. Check `tsconfig.json` for `emitDecoratorMetadata` and `experimentalDecorators`. Inspect dependency graph for circular references.
TS2304: Cannot find name 'Reflect'.
TypeScript compiler is unaware of the `Reflect` global object, likely because `reflect-metadata` is installed but not included in `tsconfig.json`'s `types` or `lib`.
fix
Add `"reflect-metadata"` to the `types` array in your `tsconfig.json`'s `compilerOptions`, or ensure `"lib": ["esnext", "dom"]` (or similar) is present which often includes `esnext.reflect`.
Upgrade
Version history
0.6.0latest on npm
Audit
Dependencies
reflect-metadatarequiredRequired for emitting decorator metadata, which TypeDI relies on heavily for dependency resolution.
Agent activity
14 hits · last 30 days
node
14
Resources
typedi — npm install typedi · libregistry