Documentation
Load service from package
Load service from package
Register services from npm packages instead of local project files.
You can load a service from an external package (npm, yarn, etc.) instead of a local project file. This is useful for wrapping Node.js built-in modules or third-party packages as container services.
YAML
Use the package name as class and the exported class name as main:
services:
foo_emitter:
class: events
main: EventEmitter
JSON
{
"services": {
"foo_emitter": {
"class": "events",
"main": "EventEmitter"
}
}
}
JS — using PackageReference
In JavaScript configuration or programmatic registration, use PackageReference to reference an npm package:
import {ContainerBuilder, Definition, PackageReference} from 'node-dependency-injection'
let container = new ContainerBuilder()
// Inject EventEmitter (from the built-in 'events' package) as a constructor argument
container
.register('app.my_service', MyService)
.addArgument(new PackageReference('events'))
PackageReference accepts the package name as it appears in node_modules (or Node.js built-ins). The container will require/import the package and inject its default export.
If the package exports a named class, specify the main property in YAML/JSON, or import it directly when registering programmatically:
import {EventEmitter} from 'events'
container.register('foo_emitter', EventEmitter)
