motdotla / dotenv

Loads environment variables from .env for nodejs projects.
https://www.dotenvx.com
BSD 2-Clause "Simplified" License
19.01k stars 853 forks source link

.env returns undefined or must a be string error in console #822

Closed wathika-eng closed 5 months ago

wathika-eng commented 5 months ago

Tried some solutions from closed issues e.g https://github.com/motdotla/dotenv/issues/546 and https://stackoverflow.com/questions/44915758/node-process-env-variable-name-returning-undefined but none seems to be working

image

Emmo00 commented 5 months ago

Hello @wathika-eng , I think the problem is the way you're running the script and the concept of "current working directory" Given this little script that checks your cwd script: image And this file Structure: image Notice the difference in output when I call the script from outside the dir directory with path dir/script.js and when I call it from inside the dir directory with script.js: image

So even when you're calling routes/router.js, your current working directory is still the project's root, so you don't need to specify the path to your .env file.

wathika-eng commented 5 months ago

Worked after deleting the path I had specified