Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

not getting data from .env file in node.js

js and i’m getting error with accessing data from .env file.

my file structure looks like this:
enter image description here

in .utils folder i have file mail.js where i need to access port and other data from .env

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

mail.js

require("dotenv").config({ path: "../.env" });
    
const port = process.env.email_port;
const host = process.env.email_host;
const user = process.env.email_user;
const pass = process.env.email_pass;

console.log("port", port); //undefined

getting undefined here.

Note: path is correct to .env and variable names are also correct.

>Solution :

Try to add require("dotenv").config() to the server app

or remove the { path: "../.env" } from the require line

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading