n8n/packages/@n8n/di
n8n-assistant[bot] caff5b7013
🚀 Release 2.40.0 (#38711)
Co-authored-by: Matsuuu <16068444+Matsuuu@users.noreply.github.com>
2026-09-15 13:02:15 +03:00
..
src chore: Update eslint configuration (#38307) 2026-09-10 14:40:35 +03:00
oxlint.config.mts build: Initialize n8n-oxlint config (#38565) 2026-09-14 07:55:26 +00:00
package.json 🚀 Release 2.40.0 (#38711) 2026-09-15 13:02:15 +03:00
README.md refactor(core): Replace typedi with our custom DI system (no-changelog) (#12389) 2025-01-06 10:21:24 +01:00
tsconfig.build.json chore: Migrate the rest of packages/@n8n to typescript 7 (#34332) 2026-07-17 10:45:58 +03:00
tsconfig.json chore: Migrate the rest of packages/@n8n to typescript 7 (#34332) 2026-07-17 10:45:58 +03:00
vitest.config.ts chore: Migrate @n8n/di from jest to vitest (#30518) 2026-05-19 10:12:01 +00:00

@n8n/di

@n8n/di is a dependency injection (DI) container library, based on typedi.

n8n no longer uses typedi because:

  • typedi is no longer officially maintained
  • Need for future-proofing, e.g. stage-3 decorators
  • Small enough that it is worth the maintenance burden
  • Easier to customize, e.g. to simplify unit tests

Usage

// from https://github.com/typestack/typedi/blob/develop/README.md
import { Container, Service } from 'typedi';

@Service()
class ExampleInjectedService {
  printMessage() {
    console.log('I am alive!');
  }
}

@Service()
class ExampleService {
  constructor(
    // because we annotated ExampleInjectedService with the @Service()
    // decorator TypeDI will automatically inject an instance of
    // ExampleInjectedService here when the ExampleService class is requested
    // from TypeDI.
    public injectedService: ExampleInjectedService
  ) {}
}

const serviceInstance = Container.get(ExampleService);
// we request an instance of ExampleService from TypeDI

serviceInstance.injectedService.printMessage();
// logs "I am alive!" to the console

Requires enabling these flags in tsconfig.json:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}