50

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?

node ninja
  • 31,796
  • 59
  • 166
  • 254
  • I've been using .env files with [dotenv](https://www.npmjs.com/package/dotenv) package and really useful but I have a trouble if I change some variable on the .env file and restart the process (e.g. using rs command on console) any new changes on the .env file are not taken in account – rkmax Nov 30 '15 at 14:56
  • There are implications with _watching_ .env file, which are documented in the FAQ: https://github.com/remy/nodemon/blob/master/faq.md#nodemon-doesnt-restart-on-env-change Some readers might come here because of friction from that. – febeling Apr 09 '22 at 11:38

16 Answers16

42
  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

Alex Montoya
  • 4,697
  • 1
  • 30
  • 31
  • 2
    hmm, this did not seem to work for me when using Typescript with nodemon, or possibly in combination with extension `-e` flag. – cevaris Feb 11 '22 at 16:10
33

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.

Danilo Cabello
  • 2,814
  • 1
  • 23
  • 26
27

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.

flipside
  • 579
  • 6
  • 11
  • There is a problem with this approach if any of your variables inside have JSON value. Example: `JSON={"a": "b"}`, because of doublecuotes escaping. – Kostanos Oct 24 '19 at 12:20
13

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.

Kara Brightwell
  • 2,529
  • 1
  • 21
  • 29
12

This will do it,

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)

Tom
  • 471
  • 3
  • 11
8

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

$ foreman run nodemon web.js
Anuj
  • 96
  • 1
  • 1
  • This is a great solution since it also doesn't force you to have a `Procfile` if you don't want one. The alternative is to use the `start` [script section](https://www.npmjs.org/doc/misc/npm-scripts.html) in your `package.json` file, which gets used by `npm start` and `nodemon`. And if you use [Heroku](http://heroku.com) for production, it now generates a `Procfile` on deployment that uses `npm start` as well. – twistedstream Nov 21 '14 at 16:02
5

In my case I'm using TypeScript and the .env file is used for development only. I wanted my code to be decoupled from the .env file and 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.

jens1101
  • 567
  • 7
  • 12
3

Thread necromancy!

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

amsross
  • 453
  • 2
  • 12
3
"scripts": {
    "start": "node -r dotenv/config src/server.js dotenv_config_path=dev.env dotenv_config_debug=true",
    "start:dev": "nodemon --exec \"npm start\""
  }
MeVimalkumar
  • 3,192
  • 2
  • 15
  • 26
2

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

Aamer
  • 121
  • 2
  • 4
2

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.

Sean
  • 629
  • 8
  • 24
1

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

Voltrun
  • 77
  • 7
1

in package.json:

...
  "scripts": {
    ...
    "dev": "nodemon src/index.js -w . -w .env -e js,mjs,json,env",
    ...
  },
...

assuming index.js is located at ./src and .env file is located at root.

npm run dev now:

[nodemon] watching path(s): *.* .env
[nodemon] watching extensions: js,mjs,json,env
ariady
  • 11
  • 2
0

Heroku Procfile

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

iancrowther
  • 5,143
  • 4
  • 25
  • 25
0

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

nodemon -r dotenv/config myapp.js
cmd
  • 11,622
  • 7
  • 51
  • 61
0

I use cross-env for environments.

npm i cross-env

set package.json.

"start": "cross-env NODE_ENV=production node dist/app.js",
"dev": "cross-env NODE_ENV=dev nodemon --exec ts-node src/app.ts",

npm run start OR npm run dev

imki123
  • 58
  • 7