n8n/packages/@n8n/di
github-actions[bot] 552c4260c0
🚀 Release 1.82.0 (#13639)
Co-authored-by: tomi <10324676+tomi@users.noreply.github.com>
2025-03-03 17:45:11 +00:00
..
src fix(core): Improve cyclic dependency check in the DI container (#12600) 2025-01-15 14:15:07 +01:00
.eslintrc.js refactor: Move vitest-config and eslint-config packages to @n8n (#13530) 2025-02-26 14:45:35 +02:00
jest.config.js fix(core): Improve cyclic dependency check in the DI container (#12600) 2025-01-15 14:15:07 +01:00
package.json 🚀 Release 1.82.0 (#13639) 2025-03-03 17:45:11 +00:00
README.md
tsconfig.build.json refactor: Move tsconfig.*.json files to separate npm package (no-changelog) (#13426) 2025-02-25 20:45:50 +02:00
tsconfig.json refactor: Move tsconfig.*.json files to separate npm package (no-changelog) (#13426) 2025-02-25 20:45:50 +02: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
  }
}