@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
import { Container, Service } from 'typedi';
@Service()
class ExampleInjectedService {
printMessage() {
console.log('I am alive!');
}
}
@Service()
class ExampleService {
constructor(
public injectedService: ExampleInjectedService
) {}
}
const serviceInstance = Container.get(ExampleService);
serviceInstance.injectedService.printMessage();
Requires enabling these flags in tsconfig.json
:
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}