---
title: "Discussion on \"Why do I have to use Dependency Injection in JS?\" | Hashnode"
description: "Discussion on \"Why do I have to use Dependency Injection in JS?\". Anytime we go into a project, existent or newly created, we always think about how what we'll build can be more manageable, scalable and and easy to test. This is where using Dependency Injection can come in handy for us.\nBut first, what do we mean b..."
url: "https://hashnode.com/posts/why-do-i-have-to-use-dependency-injection-in-js/5f4b497e3a844e59965371e4"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Why do I have to use Dependency Injection in JS?](https://vyzaldysanchez.hashnode.dev/why-do-i-have-to-use-dependency-injection-in-js?utm_source=hashnode&utm_medium=feed)

Anytime we go into a project, existent or newly created, we always think about how what we'll build can be more manageable, scalable and and easy to test. This is where using Dependency Injection can come in handy for us. But first, what do we mean b...

Aug 30, 2020 · 5 min read · vyzaldysanchez.hashnode.dev  
Updated Sep 2, 2020  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#typescript](https://hashnode.com/tag/typescript)
-   [#nodejs](https://hashnode.com/tag/nodejs)
-   [#testing](https://hashnode.com/tag/testing)
-   [#programming](https://hashnode.com/tag/programming)

## Written by

[Vyzaldy Andrés Sanchez](https://hashnode.com/@vyzaldysanchez)

@vyzaldysanchez · Dominican Republic

  
[View profile](https://hashnode.com/@vyzaldysanchez)

Software Engineer; JavaScript and Golang enthusiast; Sometimes blogger...?

I'm Vyzaldy Sanchez, a software engineer with 5+ years of experience collaborating in Software projects. I love software development and I’m a huge JavaScript and Golang enthusiast. I enjoy challenges and learning new things as much often as I can. I’m passionate about what I do in my career, knowing that I may not be the best but that I can overcome any difficulty and always be a better version of myself everyday.

## Responses 1

Markdown supported · ⌘↵ to post

[Dinys Monvoisin](https://hashnode.com/@dinmon)

[Aug 30, 2020](https://hashnode.com/posts/why-do-i-have-to-use-dependency-injection-in-js/5f4b497e3a844e59965371e4/comment/5f4b8732f540845bb26f02c8)

I was looking at the last bit of code and I was saying why are you destructuring sendNotificationsToUsers. Now, that I realise, I usually just return a function and do a call, but this way is easier.

I have one question, how would you convince your co-worker implementing this when the code base is already messed-up?

[Vyzaldy Andrés Sanchez](https://hashnode.com/@vyzaldysanchez)

[Aug 30, 2020](https://hashnode.com/posts/why-do-i-have-to-use-dependency-injection-in-js/5f4b497e3a844e59965371e4/comment/5f4b8732f540845bb26f02c8#reply-5f4be815f540845bb26f06c2)

I mostly avoid default-returning from modules TBH. I like the fact that we can forced the consumed to use a named exported value/object from the module.

Convining co-workers can be tricky, at least for this state of mind. I'll explain them how difficult things turn out to be if not done this way. One difficult convincing scenario would be trying to implement TDD(Test Driven Development).

You could start by stating how a software solution is better off by not being attached to implementations but to abstractions. If a module/function/service or whatever layer representation you would have is tied to a certain implementation, it's going to be a mess when you want to change it due to business requirements, due to the implementation not being enough, etc...

Let's say that you installed a library and you are directy using it in many layers and modules of your application, and then you find out that it has a security breach... You'll need to every place you're using it in order to change for a new one, and will have to modify big parts of your codebase to comply to the new library API. This is just one example...

Another thing I'll state is how difficult it becomes to test that, you'll require to do some "magic" in order to mock dependencies... What if you want to perform a test without calling the real database, or using a fake mailer, etc...? That easily gets messy, IMO.

In conclusion, what is important when trying to convince is to be able to demonstrate how messed up things are and how they would improve using whatever approach you are suggesting. For this case, being able to provide dependencies to the caller gives you total control on how to do things and makes testing easier in general, which is REALLY important.

I hope I was clear enough in my response. Please feel free to keep asking questions. I'm here to help.

Also, I don't hold any absolute truth, all my comments are based on previous experience and reads through out my young career, so maybe some other people might feel different around all of this.

[Dinys Monvoisin](https://hashnode.com/@dinmon)

[Aug 31, 2020](https://hashnode.com/posts/why-do-i-have-to-use-dependency-injection-in-js/5f4b497e3a844e59965371e4/comment/5f4b8732f540845bb26f02c8#reply-5f4cb82896b55f6819b7df26)

Usually what I do write the piece of code and try to convince them during the code review. Everything you say is right, but you are in a company, and other stakeholder involved like the product owner who just focused on delivery, it is hard to implement TDD.

Sometimes, when I take to much time to implement a feature I feel guilty, just to tell you that where I work they worry more about finishing on a deadline rather than producing quality code.

[Vyzaldy Andrés Sanchez](https://hashnode.com/@vyzaldysanchez)
