Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

287
Vistas
I can not use env file variable values in my node project

I have 2 variables in env file. For Port and mongoDB link. I have tried many solutions on the internet but i am not able to access the values in the listen method nor in database connection. Here is my sever file code:

const app = require("./app");
const connectDatabase = require("./config/database");

// Config
require("dotenv").config({ path: "backend/config/config.env" });

// Connecting to database
connectDatabase();

app.listen(process.env.PORT, () => {
  console.log(`Server is working on http://localhost:${process.env.PORT}`);
}); // I can run the app if i use any port number in place of "process.env.PORT"

You can see folder structure plus the variables in my env file

Note: I have put the wrong password here to hide my credentials.

I have tried to use env file in root directory also but it did not resolve the issue. The thing is i can access the values in the console but not in the listen method or in connection method.

I can run the app with direct values. App working fine with direct values

Here is the listen method error screenshot: console error for the port

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Right now you are passing 3000; to the listen function, you need to pass 3000.

Remove the semicolons ; from the env file and it should work.

Have a look at the dotenv npm page here for examples.

Also, you have posted your mongodb credentials online, I'd suggest you change those immediately.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda