Injecting a service from a different Nest.js module involves a few steps to ensure proper dependency injection and module organization. Using two sample modules, learn how the process of exporting and importing services works.
Generating a Nest.js Project
To generate a Nest.js project, you need to have the CLI installed on your device. If you don’t, run this command to install it:
With the Nest.js CLI installed, run this command to generate a new Nest.js project:

You can replace “” with any name you choose. Running the command above will generate a new Nest.js project with the specified name.
Your current project structure should look like the image below:
To practice injecting a service from one module to a different module, you’ll generate two modules, module-a and module-b. You will also generate their corresponding service and controller files.
Run this command to generate module-a:
And run the equivalent command for module-b:
Then run this command to generate the service and controller files for module-a:
Your current project directory should look like this, withsrc/module-aandsrc/module-bdirectories:
Exporting a Service From Module A
To export the module-a service from the module-a module, you have to list it as an export in module-a’s module file (module-a.module.ts). By default, the Nest.js CLI does not provide anexportsarray in the@Moduledecorator, so the generated module file will look like this:
To make service-a (module-a.service.ts) accessible to modules that import module-a, create anexportsarray in the@Moduledecorator and addModuleAServiceto it.

Next, for testing purposes, add a simple function to your module-a service file (module-a.service.ts):
This function returns a sample string. To confirm that you can import this service correctly, you’ll call that function from module-b after injecting service-a.
Importing a Service Into Module B
To import one module into another, you have to list it as an import in theimportsarray of the receiving module. In this case, you have to add module-a to theimportsarray of module-b’s@Moduledecorator.
As before, the Nest.js CLI doesn’t automatically generate animportsarray, so you must manually add it.
First, import the parent module (module-a.module.ts) into the receiving module (module-b.module.ts), create theimportsarray, and addModuleAModuleto the array:
Next, open yourmodule-b.service.tsfile and import theInjectdecorator andModuleAServericefrom@nests/commonand../module-a/module-a.service, respectively:
TheInjectdecorator marks its parameter as a target for dependency injection.
Next, in yourModuleBServiceclass, add the code block below:
The code block above gives your ModuleBService access to the methods available in your ModuleAService.
You can test the service by calling the ModuleAService’sgetHellomethod.
Next, open yourmodule-b.controller.tsfile and replace the generated code with the code block below:
The code block above sets up aGETroute handler for thegetHellofunction.
Finally,make a GET request with curlto localhost:3000/module-b/hello. The command should print “Hello from Module A!” to your console.
You have successfully injected a service into another module. This can come in handy when you arebuilding APIs with Nest.jsthat have multiple modules that need to call each other’s methods.
Benefits of Cross-Module Injection
While directly calling a service from another module might seem simpler at first, it can lead to a more complex, less maintainable, and less scalable system in the long run.
However, cross-module injection promotes code modularity and reusability, making it easier to maintain. Additionally, it centralizes dependencies, improves testability, and supports a scalable, decoupled architecture.