typedi
typedi copied to clipboard
question: Using typedi with monorepo
I was trying to... I was trying to use typedi with monorepo, where we have a common repo where we place some common stuff between other repos, so there are common services there as well.
The problem: If I create service in that common repo, decorate it with @Service decorator, I still can't inject it anywhere else, it throws ServiceNotFoundError. If I place the same services inside my repo I'm trying to access this service in - everything works just fine. However now I need to duplicate all these services in all repos which is not optimal... Is there are way to share these services between repos in monorepo structure? Any ideas would really be helpful
I’m using typedi in an nx monorepo without issues. Could you please create a simple reproduction repo on github so we can investigate?
Of course, created a public repo, you can start it up with 2 make commands from readme file. If you go to service2/src/services/Authentication.ts you will see that I try to reach Cryptography service. If I use Cryptography service defined in the same project (in the same folder in this case as well) - it works as it should, however if I use that CryptographyService from service1 - it does not... https://github.com/Kimblis/typedi-monorepo-example
It seems to me that the node_modules resolution is a bit interesting in your repo. Because you have a node_modules folder for each app, the Service decorator gets resolved from their respective folders, resulting in multiple instances of Container. In our nx monorepo, we have one shared node_modules folder so the resolution is fine.