In my case, every time I tried to get a key from the .env file using process.env.MY_KEY, it returned undefined.
I suffered from this problem for two hours just because I named the file something like keys.env which is not considered to be a .env file.
So here is the troubleshooting list:
-
The filename should be
.env(I believe.env.testis also acceptable). -
Make sure you are requiring it as early as possible in your application using this statement
require('dotenv').config(); -
The
.envfile should be in the root directory of your project. -
Follow the “file writing rules” like
DB_HOST=localhost, no need to wrap values in double/single quotes.
Also, check the documentation of the package on the NPM site.