Nodemon: Allow .env file to be specified via cli flag

Created on 19 Jul 2016  路  10Comments  路  Source: remy/nodemon

It would be very useful if a path to a .env file could be specified without the need of creating an additional nodemon.json to inject environment variables. Right now there are a number of workarounds (eg: using Foreman or running commands such as env $(cat .env) nodemon app.js) to achieve this but they are all less than optimal.
Something like nodemon --env-file .env app.js would be great!

stale

Most helpful comment

This could become too convoluted. Also, you may prefer to use dotenv-safe, which would warrant another argument like --env-sample-file.

To make use of dotenv right now in Nodemon, I'd suggest using:

nodemon --exec "node -r dotenv/config" script.js

AFAIK, there's no config in Nodemon for passing command-line options to Node, along the lines of:

node [options] script [arguments]

Maybe that's something that should be supported (or already is)?

All 10 comments

Why not use npmjs.com/dotenv to read this enviroment file instead?

You mean inside the app? I normally use .env as a placeholder for environment variables I later inject in my docker containers. Another way I sometimes work around the impossibility of specifying the env file via flag is using npmjs.com/dotenv inside a gulpfile for dev only purposes, but of course avoiding it would be much more comfortable :)

This could become too convoluted. Also, you may prefer to use dotenv-safe, which would warrant another argument like --env-sample-file.

To make use of dotenv right now in Nodemon, I'd suggest using:

nodemon --exec "node -r dotenv/config" script.js

AFAIK, there's no config in Nodemon for passing command-line options to Node, along the lines of:

node [options] script [arguments]

Maybe that's something that should be supported (or already is)?

The nodemon --exec "node -r dotenv/config" script.js trick is a valid workaround, IMHO. Thanks @yanneves.

No problem!

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

In case anyone else needs it :)

This works: nodemon --exec 'node -r dotenv/config' dist/server.js dotenv_config_path=config.env

@maxfrigge you did my day, thanks!!!

My script for developing with babel, as it is not compatible with dotenv flags:
"build": "babel src -d dist",
"dev": "nodemon --watch src/ --exec \"npm run build && node -r dotenv/config\" dist/ dotenv_config_path=config/dev.env"

it is tricky because it actually generates a new build after each change, but works great!

And if you were also using something like esm, you'd do it like this: nodemon --exec 'node -r esm -r dotenv/config' index.js

I was able to do this by providing the path option to my .env file in my execution script. I used a relative path, which worked, or you could use the path resolve() function to get an absolute path.

const { resolve } = require('path');
const envPath = resolve(process.cwd(), '../.env');

require('dotenv').config({ path: envPath });
Was this page helpful?
0 / 5 - 0 ratings

Related issues

binarykitchen picture binarykitchen  路  5Comments

jonerer picture jonerer  路  4Comments

Mohammad-Quanit picture Mohammad-Quanit  路  5Comments

dimsmol picture dimsmol  路  4Comments

jagged3dge picture jagged3dge  路  4Comments