Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I call dotenv in every node JS file?

I want to work with environment variables. Unfortunately, I am an inexperienced developer and decided very late to implement such a solution in my project.

I'm trying to inject environment variables, located in .env file, to all JS files (not all of them using environment variables, but I thought it would be faster and easier). Currently, I'm using dotenv package but it is obviously working in one file at once.

Should I use dotenv the standard way? Maybe there's a vulnerability I don't know of and that's why it is very unpopular to use environment variables this way.

if (process.env.NODE_ENV !== 'production') {
  require('dotenv').config();
}
like image 641
simplecreator Avatar asked Nov 03 '19 21:11

simplecreator


Video Answer


1 Answers

You don't need to write require('dotenv').config() in every file. Just include this as the top statement in the index.js or the main file that got executed at the very first place when you run your program.

like image 63
amit gupta Avatar answered Oct 06 '22 00:10

amit gupta