Dependency Injection in Express Application

Sergey Radzishevskii
1 min readOct 12, 2021

A while ago I wrote Do we really need dependency injection in Javascript? If you share the idea that you should use Dependency Injection in Javascript, you are welcome.

What does your Express app usually look like? Let’s take the most classic example — return a list of users.

We have 2 dependencies usersRepository and logger So how can we inject them into the Express request handler?


For those who can’t wait to code, I’ve created a demo application here. More detailed explanation is below.

Prepare our dependencies

Let’s separate Express logic from our application logic and create a new UsersController that will be responsible for user management.

And let’s add some fake UsersRepository and Logger.

Build Dependencies

Let’s add a dependency injection container.

npm i rsdi

Add a file where we build our dependencies.

Map Express router to our controller

We are almost done. Finally, let’s update our index.ts

You can find a fully configured and working express application with dependency injection here


I’m a fan of SOLID principles and try to keep my code simple. I hope my advice will help you. Happy coding. And if you like my RSDI package, please star it.



Sergey Radzishevskii

Enjoy turning complex systems into intelligible architectures chilling in my garden.