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

245
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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