0
0
NestJSframework~5 mins

Module re-exporting in NestJS

Choose your learning style9 modes available
Introduction

Module re-exporting helps share features from one module to others easily. It avoids repeating imports everywhere.

You want to group several modules and share them as one package.
You want to hide internal modules but expose only some parts to other modules.
You want to simplify imports in big projects by creating a shared module.
You want to reuse common services or components across many modules.
You want to organize your code better by controlling what is visible outside.
Syntax
NestJS
import { Module } from '@nestjs/common';
import { SomeModule } from './some.module';

@Module({
  imports: [SomeModule],
  exports: [SomeModule],
})
export class SharedModule {}

The imports array brings in the module you want to re-export.

The exports array makes that module available to others importing this module.

Examples
This example re-exports UsersModule so other modules can import SharedModule to get UsersModule features.
NestJS
import { Module } from '@nestjs/common';
import { UsersModule } from './users.module';

@Module({
  imports: [UsersModule],
  exports: [UsersModule],
})
export class SharedModule {}
This example groups two modules and re-exports them together as CoreModule.
NestJS
import { Module } from '@nestjs/common';
import { UsersModule } from './users.module';
import { AuthModule } from './auth.module';

@Module({
  imports: [UsersModule, AuthModule],
  exports: [UsersModule, AuthModule],
})
export class CoreModule {}
This example re-exports a configured module so other modules get the same config by importing SharedConfigModule.
NestJS
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';

@Module({
  imports: [ConfigModule.forRoot()],
  exports: [ConfigModule],
})
export class SharedConfigModule {}
Sample Program

This example shows a LoggerService inside LoggerModule. Then LoggerModule is re-exported by SharedModule. Finally, AppModule imports SharedModule and uses LoggerService in AppController. This way, the logger is shared without importing LoggerModule directly.

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

@Injectable()
export class LoggerService {
  log(message: string) {
    console.log('Log:', message);
  }
}

@Module({
  providers: [LoggerService],
  exports: [LoggerService],
})
export class LoggerModule {}

@Module({
  imports: [LoggerModule],
  exports: [LoggerModule],
})
export class SharedModule {}

import { Controller, Get } from '@nestjs/common';

@Controller()
export class AppController {
  constructor(private logger: LoggerService) {}

  @Get()
  getHello(): string {
    this.logger.log('Hello endpoint called');
    return 'Hello World';
  }
}

@Module({
  imports: [SharedModule],
  controllers: [AppController],
})
export class AppModule {}
OutputSuccess
Important Notes

Re-exporting only works if you add the module to both imports and exports.

Use re-exporting to keep your module imports clean and organized.

Remember to export providers or modules you want others to use.

Summary

Module re-exporting shares modules easily by importing and exporting them in one place.

It helps keep code organized and reduces repeated imports.

Use it to group common modules or services for easy reuse.