Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use environment variable in another directory in a node.js app

I have a project with folder structure is as follows,

Root > client, server

I want to use the environment variables form the .env file of my Root directory in the /Server/src, But when I try to import it in /Server/src the environment variables are undefined.

Is there a way to use those environment variables in the Server directory?

// from Root directory
const dotenv = require("dotenv");

dotenv.config();

module.exports = {
  NODE_ENV: process.env.NODE_ENV,
}
// from Server directory
const { NODE_ENV,} = require("./config");

console.log(NODE_ENV); //value returns undefined
like image 458
none Avatar asked Nov 29 '25 19:11

none


1 Answers

From the documentation here: https://www.npmjs.com/package/dotenv

You can specify a custom path in your client folder files to the .env file by going

require('dotenv').config({ path: '/custom/path/to/.env' })

:)

like image 67
CDL Avatar answered Dec 02 '25 10:12

CDL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!