Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1K
Views
Use data from .env file in main.ts - NestJS

NestJS allows us to read .env file through ConfigModule and I can do that easily in my modules with code like following

@Module({
  imports: [ConfigModule.forRoot()],
  providers: [
    NVFullNameSearchService,
    NVPartialNameSearchService,
    NVPersistService,
  ],
  controllers: [NvController],
})

But above code is more to deal within modules, how can I read content from .env file in main.ts. Say I need to set port and host for my Redis service?

const microserviceOptions = {
  name: 'plscorecard',
  transport: Transport.REDIS,
  options: {
    url: 'redis://localhost:6379',
  },
};
async function bootstrap() {
  const app = await NestFactory.createMicroservice(
    NVModule,
    microserviceOptions,
  );
  app.listen(() => {
    logger.log('NameVerification Redis microservice is listening ... ');
  });
}
bootstrap();

As you can see app is yet to be created in this case. Should I directly use dotenv? As you'd expect in any enterprise environment, I have different env files for DEV, QA, UAT and Production. What's the easiest/right way to achieve it?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Thanks for all of your support. Here's how I fixed it

**app.module.ts**
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '@nestjs/config';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: `config/${process.env.NODE_ENV}.env`,
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

With the module now having the right setting, only thing left on this is to invoke it from main.ts in a more unconventional but documented way

**main.ts**
import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { Transport } from '@nestjs/microservices';
import { AppModule } from './app.module';
import { ConfigService } from '@nestjs/config';

async function bootstrap() {
  const logger = new Logger('main');
  const app = await NestFactory.create(AppModule);
  const configService = app.get(ConfigService);
  const REDIS_HOST = configService.get<string>('REDIS_HOST');
  const REDIS_PORT = configService.get<number>('REDIS_PORT');
  const microserviceOptions = {
    transport: Transport.REDIS,
    options: {
      url: `redis://${REDIS_HOST}:${REDIS_PORT}`,
    },
  };
  app.connectMicroservice(microserviceOptions);
  const PORT = configService.get<number>('PORT');
  const environment = configService.get<string>('NODE_ENV');
  const title = configService.get<string>('ENVIRONMENT_TITLE');
  await app.listen(PORT);
  logger.log(
    `${environment}, Microservice ready to receive Redis messages in PORT - ${PORT}\n Environment - ${title}`,
  );
}
bootstrap();
over 4 years ago · Santiago Trujillo Report

0

You can avoid using dotenv using your configuration service in your main.ts with app.get() method.

import { ConfigurationService } from './core/configuration/configuration.service';

async function bootstrap() {
  const configurationService = app.get(ConfigurationService);

  await app.listen(configurationService.expressPort);
}

For more informations, mind reading the documentation about using configuration in your main.ts

over 4 years ago · Santiago Trujillo Report

0

The easiest solution is actually to just import dotenv and initiialize it immediately after. It is particulary important that these are the first two things you do in your main.ts, like this:

import * as dotenv from 'dotenv';
dotenv.config();
import ...
...
async function bootstrap(){
...
}

No config module is needed to access variables in a .env file after doing that.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!