5 Things You Must Know to Inject Service in Nest JS

Are you trying to inject a Nest JS service into another service but facing errors of dependency? Here is a complete guide on how to avoid dependency issues while injecting a service.

In nutshell, you must know about these 4 things while working with Nest JS.

  1. Don’t create controllers or services before the module with Nest JS CLI.
  2. Don’t import controllers or services in the app module.
  3. Always exports services in the relevant module that you want to inject later.
  4. Make sure you have imported the module of that service you are trying to inject.
  5. Always avoid circular dependency

Working with Nest JS CLI

In order to understand the first point, you need to understand how Nest JS CLI works. When you create a service or controller, Nest will try to find the closest module and import the controller/service into the module.

1. Don’t create controllers or services before the module with Nest JS CLI.

Now let’s assume you created a new project and there is only one module which is the app module (app.module.ts). Whenever you’ll create a new service/controller without a module, Nest will import that service/controller into the app module. So, Make Sure You Always Create Module Before Service and Controller.

2. Don’t import controllers and services into the app module

The app module is the root module for your app, you don’t need to import services and controllers of other modules into the app module. You can import services or controllers of the app module or if the other controllers/services don’t have their own module. When you want to inject Nest JS service into another service, you just have to import the modules in the app module (app.module.ts) not the services and controller.

3. Always exports services in the relevant module that you want to inject later.

When you want a service to be injected in another service, you have to export that service in the relevant module. Let’s say I have a category service and I want to use inject it into the products service. In Nest JS, if you want to make it available to another service, you have to export it as exports:[CategoryService] in the module (in this case, CategoryModule).

4. Make sure you have imported the module of the service you are trying to inject

So, how would you import a service? In Nest JS, when you import a module of another module using imports:[ModuleName], all the services you specified in the exports array will be available for that module. So, you have to make sure that you are importing modules before using the services of another module.

5. Always avoid circular dependency

In Nest JS, there may be some use cases where two services need each other. For example, CategoryService needs ProductService and ProductService also requires CategoryService. In this situation, you can use two methods explained in Nest JS documentation. Both ways are easy to understand and implement.

Conclusion

Injecting the Nest JS service into another service is tricky at first. You first need to understand how Nest JS works under the hood and what is the correct way to inject a service into another service. In the above 5 points, you learned about what are the things you must do and what need to avoid.

Latest articles

Related articles

Leave a reply

Please enter your comment!
Please enter your name here