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
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' })
:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With