I am trying to use dotenv to manage configurations for different environment. At the moment, I only have development environment. To test dotenv library, I have .env
and .env.development
files in the root folder.
In .env
, I have
PORT=5001
In .env.development
, I have
PORT=5002
In index.js
, if I have require('dotenv').config()
, .env
file is loaded and I can see process.env.PORT
is 5001.
If I have require('dotenv').config({path: '.env.' process.env.NODE_ENV})
, and I also have NODE_ENV=development node index.js
, then .env.development
is not loaded.
However, if I have require('dotenv').config({path: '.env.development'})
, .env.development
file is loaded correctly.
Can I please get some help? Thank you
CodePudding user response:
Well, you can access process.env
variables AFTER dotenv
package is executed.
So, when you pass a .env variable to it's config, it's still not available for the server.
CodePudding user response:
It is possible. I tried in my local following this post and everything worked as expected.