This content originally appeared on Bits and Pieces - Medium and was authored by Kevin Vogel
I often read the question of how to inject a service/provider from another module. In this article, I wanna explain how to deal with this inside Nest.js’s dependency inversion ecosystem by a simple project we are going to create from scratch.
By the way, I uploaded the whole project on Github.

Requirements
It’s required to have a basic understanding of Node.js and TypeScript. I will choose Visual Studio Code as my code editor. You can use whatever you prefer.
Let’s get started
First, we are going to install the Nest.js CLI, so open the terminal of your choice and type:
$ npm i -g @nestjs/cli
We initialize a new Nest.js project with its CLI. That might take up to a minute. The CLI script will ask you what package manager you want to use. For this example, I select NPM.
$ nest new nest-inject-sample
After this command is done you can open your project in your IDE. Since I use Visual Studio Code, I gonna open the project by typing:
$ cd nest-inject-sample
$ code .
My project looks like this in VSC (Visual Studio Code):

The next step is optional, but usually, I commit the original initialization, so I gonna type:
$ git add .
$ git commit -m "chore(): init nest.js"
Let’s create two modules (plus their controller and service) by Nest.js CLI. The CLI creates these modules for you while adding them to src/app.module.ts.
$ nest g mo player && nest g co player && nest g s player
$ nest g mo item && nest g co item && nest g s item
Your project tree should look like this right now:

For simplicity, let’s get rid of the *.spec.ts files by running the following command in your terminal. This step is also optional.
$ rm -rf **/*.spec.ts
Now, the project tree should look way cleaner, right?

Let’s start the application by typing inside the terminal:
$ npm run start:dev
This command means that we run the application while watching for changes. If we do a change, Nest.js is smart enough to rebuild the application automatically.
We can open the application now in our browser: http://localhost:3000
Now, let’s continue with our new modules: Item and Player. We want to inject the ItemService (of the ItemModule) inside the PlayerService (of the PlayerModule). I going to show you first how the files look, and then I show you how the files look after we’ve made our changes.
So what we going to do now is, export the ItemService inside our ItemModule.
Please change src/item/item.module.ts from:
to:
Now we exported our ItemService, which means, we can access ItemService whenever we import ItemModule, but first, we going to add some life to our ItemService by adding a method that returns a string array full of items.
Please change src/item/item.service.ts from:
to
Great. Let’s import the ItemModule inside our PlayerModule in order to get access to ItemService.
Please change src/player/player.module.ts from:
to:
Now we can inject the ItemService inside PlayerService. In addition, we add a method called findPlayerById just to add some life and to test our injection at the end.
Please change src/player/player.service.ts from:
to:
Last but not least, we add some life to our PlayerController.
Please change src/player/player.controller.ts from:
to:
That’s it! We injected ItemService inside our PlayerService and added some methods to test our project right now. Our application is still running, so we simply open the following link in our browser:
http://localhost:3000/player/find/1
The result should look something like this:

As we can see, we got a list of items, which comes directly from our ItemService, even we calling a route from our PlayerController.
You can find the whole code on Github.
Wanna see more of Nest.js?
Nest.js is a great framework, right? If you want to see more, then check out the video of Kamil Myśliwiec, the creator of Nest.js, talking about his framework in depth.
Thanks for reading my article about how to inject a service from another module. I hope you could learn something new.
Cheers!
Build component-driven. It’s better, faster, and more scalable.
Forget about monolithic apps, start building component-driven software. Build better software from independent components and compose them into infinite features and apps.
OSS Tools like Bit offer a great developer experience for building component-driven. Start small and scale with many apps, design systems or even Micro Frontends. Give it a try →
Learn more
- Building a React Component Library — The Right Way
- 7 Tools for Faster Frontend Development in 2022
- Microservices are Dead — Long Live Miniservices
Inject Service from Another Module in Nest.js was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Bits and Pieces - Medium and was authored by Kevin Vogel

Kevin Vogel | Sciencx (2022-02-18T07:55:54+00:00) Inject Service from Another Module in Nest.js. Retrieved from https://www.scien.cx/2022/02/18/inject-service-from-another-module-in-nest-js/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.