'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?



Solution 1:[1]

  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

Solution 2:[2]

I have a production Procfile with:

web: node web.js

So I have created a Procfile_dev file with:

web: nodemon web.js

And when I am at development environment I run:

$ foreman start -f Procfile_dev

It works like a charm and doesn't affect production.

Solution 3:[3]

You can get nodemon to directly use the .env with the following command

$: env $(cat .env) nodemon app.js

Be aware that you'll have to restart it if you make changes to .env and it won't like it if there are any spaces in your .env file.

Solution 4:[4]

With recent versions of Node (since io.js 1.6), you can pass it the -r flag to require a module on start. This lets you directly load .env by using nodemon's --exec:

nodemon --exec 'node -r dotenv/config'

This requires the npm package dotenv to be installed.

Solution 5:[5]

Place your local configuration variables in the .env file and run foreman along with nodemon using the following command

$ foreman run nodemon web.js

Solution 6:[6]

This works pretty well for me so far,

nodemon  -w . -w .env index.js

How it works:
"-w ." tells nodemon to watch the files in the current directory
"-w .env" tells nodemon to watch the .env file
"index.js" is just the file to run when changes occur (could be anything)

Solution 7:[7]

Thread necromancy!

Use grunt-env to load environmental variables from your heroku config.

Solution 8:[8]

In Three steps

  1. Creating the file on root folder > .env
# .env ======
PORT=5000
WHO_AM_I="Who Knows"
  1. Install the dotenv
  2. Run below command
"dev": "nodemon -r dotenv/config src/app.js"

You can access the your defined variables using > process.env.varible_name

Solution 9:[9]

If you want to run Typescript in nodemon and require a particular .env file with dotenv then you can do:

In package.json scripts:

"dev": "nodemon -r dotenv/config src/myApp.ts dotenv_config_path=/path/to/your/env/file",

And a line in nodemon.json to tell nodemon to use ts-node when encountering Typescript extensions:

"execMap": {"ts": "node -r ts-node/register"},

This is useful for using a development .env file say .env.development.local for local dev work and leave the main .env file for live production variables.

Solution 10:[10]

Use the -w key to specify nodemon what to watch additionally.

"scripts": {
    "dev": "env-cmd nodemon -w app -w *.js -w .env server.js"
}

Don't forget rerun npm run dev

Solution 11:[11]

"scripts": {
    "start": "node -r dotenv/config src/server.js dotenv_config_path=dev.env dotenv_config_debug=true",
    "start:dev": "nodemon --exec \"npm start\""
  }

Solution 12:[12]

In my case the .env file is used for development and not deployment. So I wanted my code to be decoupled from the .env file. Ideally I didn't want to import 'dotenv/config' anywhere in my code. This is my solution:

My nodemon config:

{
  "watch": [
    "src",
    ".env"
  ],
  "ext": ".ts",
  "exec": "ts-node -r dotenv/config ./src/index.ts"
}

My NPM script:

"start:dev": "nodemon"

In this solution ts-node requires dotenv, which sets up the environment variables before the main app starts. This means that nowhere in my code do I need a import 'dotenv/config'. dotenv can become a dev dependency, and this also prevents dotenv to be loaded at all once the code is deployed.

Solution 13:[13]

Heroku Procfile

Change: web: node app.js to web: nodemon app.js

Solution 14:[14]

To load the dotenv package and any declared .env vars into the environment, you can do the following:

nodemon -r dotenv/config myapp.js