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?
CodePudding user response:
try to specify exact path to your .env file like this
dotenv.config({ path: `${__dirname}/../../.env.dev` })
CodePudding user response:
this solved it
require('dotenv').config({ path: path.resolve(__dirname, '../.env') })
i already had const path = require('path')
so i just put it in