0
0
NestjsHow-ToBeginner ยท 3 min read

How to Export Provider from Module in NestJS: Simple Guide

In NestJS, to export a provider from a module, add the provider's name to the module's exports array. This makes the provider available for import in other modules that import this module via the imports array.
๐Ÿ“

Syntax

In a NestJS module, you define providers inside the providers array. To make a provider available outside the module, include it in the exports array. Other modules can then import this module to use the exported provider.

  • providers: List of services or providers created in the module.
  • exports: List of providers to share with other modules.
  • imports: Other modules this module depends on.
typescript
import { Module } from '@nestjs/common';
import { MyService } from './my.service';

@Module({
  providers: [MyService],
  exports: [MyService],
})
export class MyModule {}
๐Ÿ’ป

Example

This example shows a MyModule that provides and exports MyService. Another module, AppModule, imports MyModule and can use MyService via dependency injection.

typescript
import { Injectable, Module } from '@nestjs/common';

@Injectable()
export class MyService {
  getMessage(): string {
    return 'Hello from MyService!';
  }
}

@Module({
  providers: [MyService],
  exports: [MyService],
})
export class MyModule {}

import { Module, Controller, Get } from '@nestjs/common';
import { MyService } from './my.service';
import { MyModule } from './my.module';

@Controller()
export class AppController {
  constructor(private readonly myService: MyService) {}

  @Get()
  getHello(): string {
    return this.myService.getMessage();
  }
}

@Module({
  imports: [MyModule],
  controllers: [AppController],
})
export class AppModule {}
Output
When you run the NestJS app and visit the root URL, the response will be: Hello from MyService!
โš ๏ธ

Common Pitfalls

Common mistakes when exporting providers include:

  • Not adding the provider to the exports array, so other modules cannot access it.
  • Trying to import a provider directly without importing its module.
  • Forgetting to import the module that exports the provider in the consuming module.

Always export the provider in the module where it is declared and import that module wherever you want to use the provider.

typescript
/* Wrong: Provider declared but not exported */
@Module({
  providers: [MyService],
  // exports: [MyService], // Missing export
})
export class MyModule {}

/* Right: Provider exported properly */
@Module({
  providers: [MyService],
  exports: [MyService],
})
export class MyModule {}
๐Ÿ“Š

Quick Reference

Summary tips for exporting providers in NestJS modules:

  • Declare providers in the providers array.
  • Add providers to exports to share them.
  • Import the module that exports the provider in other modules.
  • Use dependency injection to access exported providers.
โœ…

Key Takeaways

Add providers to the module's exports array to share them with other modules.
Import the module that exports the provider to use it elsewhere.
Providers not exported remain private to their module.
Use dependency injection to access exported providers in consuming modules.