5

Inject Service from another Module in Nest.js (2022)

 2 years ago
source link: https://blog.bitsrc.io/nest-js-inject-service-from-another-module-cf85987398d5
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Inject Service from another Module in Nest.js (2022)

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.

Screenshot of Nest.js
Nest.js Website

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 code editor. Since I use Visual Studio Code, I gonna open the project by typing:

$ cd nest-inject-sample
$ code .

My project looks like this in VSCode (Visual Studio Code):

Nest.js Project Tree
Nest.js Project Tree

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 --no-spec && nest g s player --no-spec
$
nest g mo item && nest g co item --no-spec && nest g s item --no-spec

Your project tree should look like this right now:

Nest.js Project Tree
Nest.js Project Tree

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:

src/item/item.module.tssrc/item/item.module.ts

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:

src/item/item.service.tssrc/item/item.service.ts

Great. Let’s import the ItemModule inside our PlayerModule in order to get access to ItemService.

Please change src/player/player.module.ts from:

src/player/player.module.tssrc/player/player.module.ts

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:

src/player/player.service.tssrc/player/player.service.ts

Last but not least, we add some life to our PlayerController.

Please change src/player/player.controller.ts from:

src/player/player.controller.tssrc/player/player.controller.ts

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:

1*O6uRACKH1iMofe0k2hyYgw.png?q=20
nest-js-inject-service-from-another-module-cf85987398d5

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 →

0*hX30HLO0QBJky-kT?q=20
nest-js-inject-service-from-another-module-cf85987398d5
An independent product component: watch the auto-generated dependency graph

Learn more


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK