Multiple Environments

Run any environment locally. Create a `.env.ENVIRONMENT` file and use `-f` to load it. It's straightforward, yet flexible.

--env-file

$ echo "HELLO=production" > .env.production 
$ echo "console.log('Hello ' + process.env.HELLO)" > index.js

$ dotenvx run -f=.env.production -- node index.js
Hello production
> ^^

--env-file (multiple)

$ echo "HELLO=local" > .env.local
$ echo "HELLO=World" > .env
$ echo "console.log('Hello ' + process.env.HELLO)" > index.js

$ dotenvx run -f=.env.local -f=.env -- node index.js
Hello local

The order matters. The first -f will 'win' for an environment variable. You can use --overload if you prefer the last to 'win'.

--overload

--overload

$ echo "HELLO=local" > .env.local
$ echo "HELLO=World" > .env
$ echo "console.log('Hello ' + process.env.HELLO)" > index.js

$ dotenvx run -f=.env.local -f=.env --overload -- node index.js
Hello World

Keep in mind that --overload will also overload any environment variables already set on your machine or server. Use --verbose to get better visibility into this.

--verbose

--verbose

$ echo "HELLO=production" > .env.production
$ echo "console.log('Hello ' + process.env.HELLO)" > index.js

$ dotenvx run -f=.env.production --verbose -- node index.js
┆ loading env from .env.production (/path/to/.env.production)
┆ HELLO set
⟐ injected env (1) from .env.production
Hello production

If you also want to see the secret value being set, use --debug.

--debug

--debug

$ echo "HELLO=production" > .env.production
$ echo "console.log('Hello ' + process.env.HELLO)" > index.js

$ dotenvx run -f=.env.production --debug -- node index.js
┆ loading env from .env.production (/path/to/.env.production)
┆ {"HELLO":"production"}
┆ HELLO set
┆ HELLO set to production
⟐ injected env (1) from .env.production
Hello production

Be careful with --debug as this will leak your secrets out to your logs unless you've configured your logs to avoid it.