Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use nodemon with .env files?

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?

like image 397
node ninja Avatar asked May 12 '12 01:05

node ninja


People also ask

Where do I put .env files?

You can create an. env file in the application's root directory that contains key/value pairs defining the project's required environment variables. The dotenv library reads this.

How do .env files work?

env file contains the individual user environment variables that override the variables set in the /etc/environment file. You can customize your environment variables as desired by modifying your . env file.


1 Answers

  1. Install dotenv npm i dotenv
  2. Create .env file and your variables inside
  3. Add the script to execute

    "dev": "nodemon -r dotenv/config ./app/index.js " or "start": "node -r dotenv/config ./app/index.js " 
  4. Run the app using npm run dev or npm run start

like image 168
Alex Montoya Avatar answered Sep 23 '22 23:09

Alex Montoya