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

248
Vistas
environnement variable not loading using javascript, node and express

I want to store variables in my .env file and use them instead of using my sensitive data in the code.

require("dotenv").config();
const express = require("express");
//call express methode to creat the app
const app = express();
//mongoose import to work with the data base
const mongoose = require("mongoose");
//get access to path
const path = require("path");
//intercept any request containing json() content and put it in the request body (same as body parser)
app.use(express.json());
//=======data base connection========
//store our values away, in the .env

const mongUser = process.env.USER_MON;
console.log(mongUser);

Here is the code. (there is some other stuff lower but it's this part that is not working.

When I try to console log the general env with console.log(process.env) I get everything except what is inside my env file (at the root of the repo):

JWT_KEY=HYinity764knn9
USER_MON=myUserName

I can't see those variable in the console, i don't understand why they aren't loading. what am I missing?

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

0

try to specify exact path to your .env file like this

dotenv.config({ path: `${__dirname}/../../.env.dev` })
about 4 years ago · Juan Pablo Isaza Denunciar

0

this solved it

require('dotenv').config({ path: path.resolve(__dirname, '../.env') })

i already had const path = require('path') so i just put it in

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