How to use nodemon with .env files?

development-environment, environment-variables, javascript, node.js, web-services

Solution

- Install dotenv `npm i dotenv`

- Create `.env` file and your variables inside

Add the script to execute

"dev": "nodemon -r dotenv/config ./app/index.js " or
"start": "node -r dotenv/config ./app/index.js "

Run the app using `npm run dev` or `npm run start`

Problem

I am using an .env file to hold environment variables for the server. This works if I run the server with foreman start. But it doesn't work with nodemon. I would like to use nodemon instead because it restarts automatically when you modify the server. How can I get nodemon to work with .env files?

Original source