TypeDI not detecting Services from local external package

886 views Asked by At

I have a mono repo project that consists of two (lerna built) packages Base and Libs. I am trying to use TypeDi dependency injection and the classes marked with the Service() decorator from the Libs project are not created in the Base Container:

libs/example.js

    import { Service } from 'typedi';

    @Service()
    export class ExampleService{
        //...do stuff
    }

libs/index.js

    import { ExampleService } from './example';

    export { ExampleService };

base/index.js

    import { Container } from 'typedi';
    import { ExampleService } from 'Libs'

    //Error thrown here "Service with "MaybeConstructable<ExampleService>" identifier was not found in the container. Register it before usage via explicitly calling the "Container.set" function or using the "@Service()" decorator."
    const xmpl = Container.get(ExampleService)

Is there any way to inject these classes without explicitly importing all the class dependencies to the Base project and using Container.set()

1

There are 1 answers

2
PIoneer_2 On

According to the error message ExampleService was not registered.

The code example lacks the required import 'reflect-metadata'; string at the entry point of your application.

Add the reflect-metadata as the first string to your app/server/... ts file.