How to inject service from another module into nestjs modules

This tutorial explains how to inject Service from one mon=dule to another module in the NestJS application with an example.

In another way, how to share and export a service and import it into another module.

How to Inject Service from one module to another module

For example, I created EmployeeModule and DepartmentModule.

  • Create Employee Module, Controller, and Service.

First, Create an EmployeeModule using the below command

A:\work\nestjs-hello-world>nest generate module employee
CREATE src/employee/employee.module.ts (85 bytes)
UPDATE src/app.module.ts (326 bytes)

The module is created as follows

import { Module } from "@nestjs/common";
import { EmployeeModule } from "./employee/employee.module";

@Module({
  imports: [EmployeeModule],
  controllers: [],
  providers: [],
})
export class AppModule {}

Next, Create an EmployeeController using the below command

A:\work\nestjs-hello-world>nest generate co Employee
CREATE src/employee/employee.controller.ts (105 bytes)
CREATE src/employee/employee.controller.spec.ts (506 bytes)
UPDATE src/employee/employee.module.ts (182 bytes)

a controller and test spec file created as given below.

import { Controller } from "@nestjs/common";

@Controller("employee")
export class EmployeeController {}

Also, updates the controller section in employee.module.ts

import { Module } from "@nestjs/common";
import { EmployeeController } from "./employee.controller";

@Module({
  controllers: [EmployeeController],
})
export class EmployeeModule {}

Next, Create an EmployeeService using the below command

A:\work\nestjs-hello-world>nest generate service Employee
CREATE src/employee/employee.service.ts (92 bytes)
CREATE src/employee/employee.service.spec.ts (474 bytes)
UPDATE src/employee/employee.module.ts (268 bytes)

service and spec file created

import { Injectable } from "@nestjs/common";

@Injectable()
export class EmployeeService {}

Also, updated this service file in the Module file(employee.module.ts)

import { Module } from "@nestjs/common";
import { EmployeeController } from "./employee.controller";
import { EmployeeService } from "./employee.service";

@Module({
  controllers: [EmployeeController],
  providers: [EmployeeService],
})
export class EmployeeModule {}

Let’s add some dummy method, that returns a string in Employee Service.

import { Injectable } from '@nestjs/common';

@Injectable()
export class EmployeeService {

    public getEmployee(): String{
        return "Employee object"
    }

}
  • Create Department Module, Controller, and service

First, create department a module

A:\work\nestjs-hello-world>nest g module department
CREATE src/department/department.module.ts (87 bytes)

Next, create department a controller

A:\work\nestjs-hello-world>nest g controller department
CREATE src/department/department.controller.ts (109 bytes)
CREATE src/department/department.controller.spec.ts (520 bytes)
UPDATE src/department/department.module.ts (190 bytes)

Next, create department a service

A:\work\nestjs-hello-world>nest g service department
CREATE src/department/department.service.ts (94 bytes)
CREATE src/department/department.service.spec.ts (488 bytes)
UPDATE src/department/department.module.ts (282 bytes)

Created Employee and Department modules.

Now, Let’s do EmployeeService from EmployeeModule injected into DepartmentModule.

The first step is -Make EmployeeService exportable in the Employee module added exports: [EmployeeService] to the @module section of EmployeeModule. It means, EmployeeService is declared as exported and can be ready to access in other modules. employee.module.ts:

import { Module } from "@nestjs/common";
import { EmployeeController } from "./employee.controller";
import { EmployeeService } from "./employee.service";

@Module({
  controllers: [EmployeeController],
  providers: [EmployeeService],
  exports: [EmployeeService],
})
export class EmployeeModule {}

Next Step, EmployeeService is declared in EmployeeModule, so import EmployeeModule into DepartmentModule. added imports: [EmployeeModule] to DepartmentModule.

department.module.ts:

import { Module } from "@nestjs/common";
import { EmployeeModule } from "src/employee/employee.module";
import { EmployeeService } from "src/employee/employee.service";
import { DepartmentController } from "./department.controller";
import { DepartmentService } from "./department.service";

@Module({
  imports: [EmployeeModule],
  controllers: [DepartmentController],
  providers: [DepartmentService],
})
export class DepartmentModule {}

Now, you are ready to use EmployeeService in department modules and controller code.

EmployeeService is injected into the constructor. use service class inside controller methods.

import { Controller, Get } from "@nestjs/common";
import { EmployeeService } from "src/employee/employee.service";

@Controller("department")
export class DepartmentController {
  constructor(private employeeService: EmployeeService) {}

  @Get()
  getEmployeeDepartment(): string {
    return this.employeeService.getEmployee();
  }
}

Conclusion

If you want to use Service in another module First export Service Next, Import the service module into another module