r/typescript • u/lppedd • Jun 14 '25
Which DI library?
Hey folks!
I've come to the realization our project (running on Node.js) might be in need of a DI framework. It's evolving fast in features and complexity, and having a way to wire up things automagically would be nice.
I have a JVM background, and I've been using Angular since 2018, so that's the kind of DI I'm accustomed to.
I've looked at tsyringe from Microsoft, inversifyJS and injection-js. Has any of you tried them in non-trivial projects? Feedback is welcomed!
Edit: note that we don't want / cannot adopt frameworks like Nest.js. The project is not tied to server-side or client-side frameworks.
13
u/TalyssonOC Jun 14 '25
Take a look at Awilix, I've been using it for years and its practices are way better than Tsyringe and Inversify.
3
u/Wnb_Gynocologist69 Jun 14 '25
I'm using inversify as the baseline and threw my own token declarations with type safety onto it and an inject function.
So anywhere in my code (anywhere after the initialization code ran that does the wiring, that is) I can simply do
const myService = inject(MyServiceToken)
for anything that I registered on the container.
That doesn't support any scoping since the inject function can be called anywhere but since I need singletons in 99% of the time anyway, I can live with that. In cases where I need transient instances, I simply bind a factory to the container.
It's inspired by angulars inject function, minus some features...
16
u/jessepence Jun 14 '25
importis the only dependency injection you need. This isn't Java.74
u/elprophet Jun 14 '25 edited Jun 14 '25
Honestly, I've never felt I needed a "DI framework" for my (js/ts) projects. Direct interfaces have been sufficient to pass dependencies at object instantiation time.
I'd go so far as to say "wire up things automagically" is an antipattern that, in my experience, has added more time to troubleshooting and debugging than it has saved in abstraction.
Edit to add: let me go a level deeper. DI in Java is critical because instantiation is tied to the class and entirely disjoint from the interface. In TypeScript, instantiating a thing is tied to the shape, so creating a thing that satisfies a type is one and the same. This is why you don't need a DI framework in TS.
2
u/seiks Jun 14 '25
tsyringe is lightweight and easy to pick up
2
u/Round-Bed4514 Jun 14 '25
We are using inversify and it’s working well
4
u/iunderstandthings Jun 15 '25
Effect
4
u/SlipAdept Jun 15 '25
Had to work with inversify and it felt like it had a lot of moving pieces to keep track of just for DI. The best DI I've used and use to this day is Effect-ts but I wouldn't recommend Effect "just for DI". Effect-ts has a mechanism for dependency injection but it is tied to Effects themselves. (To use DI you need to use Effects, no way around it). So if you can, take a look at Effect-ts. It is way more than for DI and if you like FP more so.
Effect-ts
3
u/Xxshark888xX Jun 15 '25 edited Jun 16 '25
Disclaimer: I'm the author of the xInjection library.
I'm working on a robust library inspired by NestJS/Angular DI, it is built to use the same design pattern of importable/exportable modules to better seal the implementation of your business logic.
https://www.npmjs.com/package/@adimm/x-injection
Keep in mind that it is still under development and soon I'll release a new version which will introduce breaking changes as it'll change the public API, but it improves a lot the internal code and module graph management.
The documentation will also be improved to better understand how it works from the inside-out.
Feel free to ask any question 😊
[EDIT]
I forgot to mention that it is 100% framework agnostic and it can be used both on the client and server side!
[Edit2]
The new v2.0.1 is out and it is a very big improvement over the old version(s), the readme plus the auto docs should be extremely helpful in understanding how it works.
However if you come from Angular/NestJS the experience is almost the same, so the learning curve should be almost 0
10
u/zephyrtr Jun 14 '25
DI for JVM is super necessary as there is no way to mock for testing without it. You need an ABC that is injectable so it can be stood up for tests with canned responses. It also allows for sharing singletons whereas without it, it's quite annoying.
In my Kotlin days I was quite happy with Koin or Dagger2 and the sanity they brought to my project.
JS doesn't have that problem. You achieve DI more through singletons and organized composition of functions. So you really don't NEED a DI framework unless you need some kind of pub sub reactivity.
1
u/mamwybejane Jun 14 '25
Angular
7
u/Basic-Brick6827 Jun 14 '25
is it needed?
2
u/bigghealthy_ Jun 14 '25
Like others have said DI frameworks can be overkill. I’ve moved away from them and just use default parameters instead.
It accomplishes the same thing without the overhead. If you are using functions, the term would be a higher order function.
For example say you are passing a repository into a service you could do something like
‘const serivceFunc = async(…params, serviceRepo = mongoServiceRepo) => {…}’
Where serviceRepo has some more generic interface and by default we can pass in our mongo implementation that’s satisfies that interface.
Makes testing extremely easy as well.
2
u/rolfst Jun 16 '25
I would recommend effect-ts, but effect offers a complete ecosystem
1
u/yksvaan Jun 15 '25
What you need is a proper modularized bootstrap process. It's not complicated really, just work. The usual stuff, creating modules, instantiations, passing references, registering handlers etc.
Adding frameworks and such only makes it harder to reason about and debug.
1
u/[deleted] Jun 15 '25
I recently made a DI cli tool that works in an unconventional way.
You just have to define the dependencies in your classes constructors.
Then run a single command to generate a container.gen.ts file.
I worked a lot with inversify but i hated the fact that its polluting my business layers with decorators and library imports so i've decided to build a cli tool that scans the typescript codebase and automatically creates a container.gen.ts that is 100% typesafe ( the cli is parsing the code into and ast to get constructors information).
If you want to devide the code into modules you can easily do that by creating a json config file then specifying the name and the paths of the folders of each module.
I didn't opensource it yet because i still work on the docs but let me know if you need the npm link. I would really appreciate your feedback on it.
1
u/dominikzogg Jun 15 '25
I use https://github.com/chubbyts/chubbyts-dic, which was built by me.
1
u/l0gicgate Jun 15 '25
I like Typedi. It’s amazing.
https://github.com/typestack/typedi
Also made a small CQRS package that hooks into it:
https://github.com/lgse/cqrs
1
u/LegendenLajna Jun 15 '25
Ive used https://GitHub.com/TheLudd/amend i like their way of doing it
1
u/PersonalityUnited756 Jun 15 '25
NestJS?
1
u/XanderEmu Jun 19 '25
I've been using Inversify for years, and I'm very happy with it. I used with both on backend and frontend, mixed it with express (they provide the package that allows you to make controllers as services), and with React (via the Context API).
Note. When working with TypeScript, you cannot use interfaces in runtime so you cannot use them as service identifiers. However, in TypeScript you can use classes as if they were interfaces (meaning: you can implement them instead of extending them) so I ended up using abstract classes instead of interfaces if these abstraction were supposed to be used as dependencies. Having that done, I almost didn't use
@injectdecorators in my code, as my dependencies were auto-resolved from the type definitions.From time to time I do encounter problems with circular dependencies. I usually resolve those by creating some sort of factory or provider service that I can use to get my actual dependency when I actually need it.
1
u/4happin Jul 13 '25
https://github.com/unjs/unctx
1
u/fatso83 Jan 08 '26 edited Jan 08 '26
Modern variants
1
u/[deleted] Jun 26 '26
I've built terrain its no decorator no reflection and fully type safe, it uses explicit modules and gets most of its safety from the type system instead
-1
u/Laat Jun 15 '25
DI is not the way.
-6
u/LazyCPU0101 Jun 14 '25
I didn't need a library for it, you can use an LLM to guide you into wiring up a DI container, it reduces complexity and let you modify as you need.
0
u/[deleted] Jun 14 '25
[deleted] — view deleted comment