Reputation: 210
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?
Upvotes: 5
Views: 5182
Reputation: 1
I discovered the solutions here is implementation in my module file
import { Module } from '@nestjs/common';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [
ConfigModule.forRoot({ envFilePath: '.env' }), // Configure ConfigModule
ClientsModule.register([
{
name: 'SERVICE_A',
transport: Transport.TCP,
options: (configService: ConfigService) => ({
host: configService.get('SERVICE_A_HOST') as string,
port: Number(configService.get('SERVICE_A_PORT')),
}),
},
{
name: 'SERVICE_B',
transport: Transport.TCP,
options: (configService: ConfigService) => ({
host: configService.get('SERVICE_B_HOST') as string,
port: Number(configService.get('SERVICE_B_PORT')),
}),
},
]),
],
controllers: [AppController],
providers: [AppService],
})
Upvotes: 0
Reputation: 210
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],
},
]),
Upvotes: 16