Mevia
Mevia

Reputation: 1564

nestjs env variable undefined before class definition

I am working on app written in nestjs and trying to implement WebSocket. One problem i am having is with use of env variables. In the gateway file where i define my WebSocket implementation we have need of using PORT from env before class is defined:

console.log(process.env.WSPORT) // undefined
setInterval(() => {
  console.log((process.env.WSPORT) // after few logs its becoming accessible
}, 100)
@WebSocketGateway(process.env.WSPORT)
export class ExportFeedsGateway implements OnGatewayInit {

I tried to debug what is going on, and it seems to be related with when it is invoked as few moments later this variable becomes available for use.

app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '@nestjs/config';
import { ExportFeedsGateway } from './modules/exportFeeds/export-feeds.gateway';

@Module({
    imports: [ConfigModule.forRoot({ expandVariables: true })],
    controllers: [AppController],
    providers: [AppService, ExportFeedsGateway],
})
export class AppModule {}

export-feeds.gateway.ts

import { OnGatewayInit, WebSocketGateway } from '@nestjs/websockets';

@WebSocketGateway(process.env.WSPORT)
export class ExportFeedsGateway implements OnGatewayInit {
    ...
}

how to modify this code to make sure that WSPORT is not undefined when its passed to WebSocketGateway decorator?

----- EDIT

I was hoping to utilise nestjs code to access this variable rather then use packages like dotenv

main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { WsAdapter } from '@nestjs/platform-ws';

const PORT = process.env.PORT;

async function bootstrap() {
    const app = await NestFactory.create(AppModule, { cors: true });
    app.useWebSocketAdapter(new WsAdapter(app));
    await app.listen(PORT);
}
bootstrap();

Upvotes: -1

Views: 923

Answers (2)

Mevia
Mevia

Reputation: 1564

I did deeper research and the problem i was having is actually result of trying to use WebSocket implementation (gateway) as standalone import. So the answer and solution to the problem is to create module first

import { Module } from '@nestjs/common';
import { ExportFeedsGateway } from './export-feeds.gateway';

@Module({
    providers: [ExportFeedsGateway],
    exports: [ExportFeedsGateway],
})
export class ExportFeedsModule {}

This is a proper way of doing things as far as nestjs is concerned. After this step module should be imported instead of gateway and env variables are accessible through process.env.something

@WebSocketGateway(Number(process.env.EXPORT_FEEDS_PORT))
export class ExportFeedsGateway implements OnGatewayInit {
    ...
}

Upvotes: -1

Micael Levi
Micael Levi

Reputation: 6685

you need to inject that env var in the shell, not via .env. Or use the dotenv by yourself as the first line at your main.ts

Upvotes: 1

Related Questions