nestjsnestjs-config

How to use environment variables in ClientsModule?


I'm trying to use environment variables in the ClientsModule as such:

import { Module } from '@nestjs/common';
import { ClientsModule, Transport } from '@nestjs/microservices';

@Module({
  imports: [
    TypeOrmModule.forFeature([Process]),
    ClientsModule.register([
      {
        name: 'PROCESS_SERVICE',
        transport: Transport.RMQ,
        options: {
          queue: process.env.RMQ_PRODUCER_QUEUE_NAME,
          urls: [process.env.RMQ_PRODUCER_URL],
          queueOptions: { durable: true },
        },
      },
    ]),

And I've also tried this:


import { Module } from '@nestjs/common';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { ConfigService } from '@nestjs/config';

const configService = new ConfigService();
const rmqProcessUrl = configService.get<string>('RMQ_PRODUCER_URL');
const rmqProcessQueue = configService.get<string>('RMQ_PRODUCER_QUEUE');

@Module({
  imports: [
    TypeOrmModule.forFeature([Process]),
    ClientsModule.register([
      {
        name: 'PROCESSES_SERVICE',
        transport: Transport.RMQ,
        options: {
          queue: rmqProcessQueue,
          urls: [rmqProcessUrl],
          queueOptions: { durable: true },
        },
      },
    ]),

But in both occasions the following error appears:

TypeError: metatype is not a constructor

It works as intended when I use the values directly. I have also tried importing and using

export const rmqServiceName = process.env.RMQ_PRODUCER_QUEUE_NAME

and

export const rmqServiceName = process.env.RMQ_PRODUCER_URL

but that also results in the same error.

So how can I get access to the .env variables in the @ClientsModule? Is there a workaround that I'm missing?


Solution

  • I discovered what is wrong, in case anyone has the same problem as I did.

    What should be done is import the nestjs/config and ConfigModule in the app.module.ts and use the async method of ClientsModule.register.

    In the app.module.ts

    import { Module } from '@nestjs/common';
    import { ConfigModule } from '@nestjs/config';
    
    @Module({
      imports: [
        ConfigModule.forRoot({ isGlobal: true }),
    

    In the module I was working on:

    import { Module } from '@nestjs/common';
    import { ClientsModule, Transport } from '@nestjs/microservices';
    import { ConfigModule, ConfigService } from '@nestjs/config';
    
    @Module({
      imports: [
        TypeOrmModule.forFeature([Process]),
        ClientsModule.registerAsync([
          {
            name: 'PROCESSES_SERVICE',
            imports: [ConfigModule],
            useFactory: async (configService: ConfigService) => ({
              transport: Transport.RMQ,
              options: {
                queue: configService.get<string>('RMQ_PRODUCER_QUEUE'),
                urls: [configService.get<string>('RMQ_PRODUCER_URL')],
                queueOptions: { durable: configService.get<boolean>('RMQ_PRODUCER_QUEUE_DURABLE') },
              },
            }),
            inject: [ConfigService],
          },
        ]),