Saturday 6 May 2017

NodeJs and Dotenv - Running nodemon does not find .env file in src folder

I've got a pretty standard Nodejs setup using dotenv with my server.js file sitting in an src folder, a .env file sitting right next to it, along with a bin/www.js file. Ever since I started using dotenv, running all of the following no longer work... nodemon (from the root of my repo) npm start nodemon src/server nodemon src etc

All fail and none of my environment variables get loaded properly. I have to go to the actual src folder and run "nodemon server.js" at that exact location. This poses problems for some of the build environments I used to use and some of my previous cloud setups like heroku. I thought having the .env file in the same folder as my server.js file would always work, but it does not.

Can anyone tell me why dotenv fails to find the .env file and load the environment variables depending on where I launch nodemon from, even though nodemon finds the server.js file just fine?

I basically would like to be able to launch nodemon from my repository's root folder and still have dotenv work. Also, I tried moving the .env folder into the root directory, and I still have problems.



via Tim Hardy

No comments:

Post a Comment