Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

250
Visualizações
Reading PORT number from .env file in nestjs

I have details defined in .env file. Below is my code.

import { Module } from '@nestjs/common';
import { MailService } from './mail.service';
import { MailController } from './mail.controller';
import { MailerModule } from '@nestjs-modules/mailer';
import { HandlebarsAdapter}  from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter';
import { join } from 'path';


@Module({
  imports:[
     MailerModule.forRoot({
      transport:{
        host: process.env.SMTP_HOST,
        port: 1025,
        ignoreTLS: true,
        secure: false,
        auth:{
          user:'shruti.sharma@infosys.com',
          pass:'Ddixit90@@',
        },
      },
      defaults:{
           from: '"No Reply" <no-reply@localhost>',
      },
      template:{
        dir:join(__dirname,'templates'),
        adapter: new HandlebarsAdapter(),
        options:{
          strict:false,
        },
      }
     }
      
     )
  ],
  controllers: [MailController],
  providers: [MailService],
  exports:[MailService]
})
export class MailModule {}

host: process.env.SMTP_HOST is working properly. but when I am writing process.env.SMTP_PORT it is saying you can not assign string to number. enter image description here

when I wrote parseInt(process.env.SMTP_PORT) it is still not working. how to assign port from .env file

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

port: +process.env.SMTP_PORT should work casting it to a number. If there is something different, perhaps share the error you are getting.

An alternative would be to use the config service for the MailerModule and use the factory. Here is an example implementation of that:

MailerModule.forRootAsync({
            useFactory: async (config: ConfigService) => ({
                transport: {
                    host: process.env.SMTP_HOST,
                    port: 1025,
                    ignoreTLS: true,
                    secure: false,
                    auth: {
                        user: "shruti.sharma@infosys.com",
                        pass: "Ddixit90@@",
                    },
                },
                defaults: {
                    from: '"No Reply" <no-reply@localhost>',
                },
                template: {
                    dir: join(__dirname, "templates"),
                    adapter: new HandlebarsAdapter(),
                    options: {
                        strict: false,
                    },
                },
            }),
            inject: [ConfigService],
        }),

Doing it this way, config.get is able to determine the type by the property. Else you can define it with config.get<number>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda