# Modules Re-Exporting in NestJS

## **Introduction**

In previous blogs, we learned how to **export providers** (like `CatsService`) from one module and then **import them into another module**. This is the standard way to share services across different parts of your NestJS application.

But what if we want to go one step further?  
Instead of importing from the original module every time, we can **re-export a module**. This makes it possible to access the same providers indirectly through another module.

### What is Module Re-Exporting?

Module re-exporting works like a chain. Think of it like a math formula:

`A = B = C`

Here’s how it works in NestJS:

1. We export something from Module A (`CatsModule`).
    
2. We import it into Module B (`RequestModule`).
    
3. Then, we export the same thing again from Module B.
    
4. Now, Module C (`ResponseModule`) can just import Module B and automatically get access to the providers from Module A.
    

So essentially, re-exporting allows us to “**forward**” dependencies from one module to another, creating a dependency chain.

### Step 1: Exporting from CatsModule

We start by exporting the `CatsService` from `CatsModule`:

```typescript
javsascript
import { Module } from '@nestjs/common';
import { CatsService } from './cats.service';
import { CatsController } from './cats.controller';

@Module({
  controllers: [CatsController],
  providers: [CatsService],
  exports: [CatsService], // 👈 exported
})
export class CatsModule {}
```

### Step 2: Import and re-export in RequestModule

Now, instead of just using `CatsService` inside `RequestModule`, we also **re-export** it:

```typescript
import { Module } from '@nestjs/common';
import { RequestController } from './request.controller';
import { CatsModule } from '../cats/cats.module';

@Module({
  controllers: [RequestController],
  imports: [CatsModule],   // 👈 imported from CatsModule
  exports: [CatsModule],   // 👈 re-exported to other modules
})
export class RequestModule {}
```

### Step 3: Import request module in response module

Finally, in `ResponseModule`, we don’t need to import `CatsModule` directly. Instead, we just import `RequestModule`, and we’ll automatically have access to `CatsService` because it was re-exported.

```typescript
import { Module } from '@nestjs/common';
import { ResponseController } from './response.controller';
import { RequestModule } from '../request/request.module';

@Module({
  controllers: [ResponseController],
  imports: [RequestModule], // 👈 gets CatsService via re-export
})
export class ResponseModule {}
```

### Using the Service in Controllers

```typescript
// request.controller.ts
import { Controller, Get } from '@nestjs/common';
import { CatsService } from '../cats/cats.service';

@Controller('request')
export class RequestController {
  constructor(private readonly catsService: CatsService) {}

  @Get()
  findAll() {
    return this.catsService.findAll();
  }
}
```

```typescript
// response.controller.ts
import { Controller, Get } from '@nestjs/common';
import { CatsService } from '../cats/cats.service';

@Controller('response')
export class ResponseController {
  constructor(private readonly catsService: CatsService) {}

  @Get()
  findAll() {
    return this.catsService.findAll();
  }
}
```

### Conclusion

Module re-exporting in NestJS is a powerful way to **share providers across multiple modules without repeatedly importing the original module**.  
You export a provider from one module, import and re-export it in another, and then any module that imports the second one automatically gets access.

Think of it as **forwarding dependencies** — simple but effective for keeping your code organised in large applications.

If you’re a visual learner, check out the following video tutorial for this blog:

[https://www.youtube.com/watch?v=O71Aeqbv3cc&list=PLgKb0PcT0J0SaWTrS4ZY8ATMaqLeSXgM7&index=13](https://www.youtube.com/watch?v=O71Aeqbv3cc&list=PLgKb0PcT0J0SaWTrS4ZY8ATMaqLeSXgM7&index=13)

Follow us for more such content:

[https://www.linkedin.com/company/hasabtech](https://www.linkedin.com/company/hasabtech)

[https://www.youtube.com/@hasabTech](https://www.youtube.com/@hasabTech)

[https://www.instagram.com/hasab.tech/](https://www.instagram.com/hasab.tech/)

[https://www.facebook.com/hasabTech](https://www.facebook.com/hasabTech)
