How to set Environment variables and avoid undefined errors

preview_player
Показать описание
Writing Environment Variables can be tricky sometimes. If you face problems in setting and accessing environment variables, this video is for you.

Quick tips:
1) If you are setting your .env file in the root of your project, you can access env variables with....
require('dotenv').config()

2)if you are setting your .env file anywhere else (other than root of the project). you can access env variables with.....
require('dotenv').config({ path: '/custom/path/to/.env' })
Рекомендации по теме
Комментарии
Автор

if it's still not working just mention the file path in the require("dotenv").config( { path : "env file location"} );

pYthonroasT
Автор

thnx senpai, 2nd option worked for me

mdmuktadirmazumder
Автор

in my case, i have a folder (models) in my project, that theris a database configuration archive. how can i do this? the result is being "undefined", but at the "index.js" main archive it works properly.

JoaoVitor-pcps
Автор

If somebody is dummy just like me and gets the "port undefined" problem... start the server while you are placed in the root folder of the project, and not from inside /root_folder/src , oh my god -.-"

o.jc.o