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

243
Views
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 answers
Answer question

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 Report

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