/ Search - type / Modes - type M Star on GitHub - 5.7k

Docs

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,.env -- node index.js
Hello local

Comma-separate multiple files after a single -f. The order matters: the first file will 'win' for an environment variable. You can use --overload if you prefer the last to 'win'.

Watch it in action

--overload

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

$ dotenvx run -f=.env.local,.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

$ 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

$ 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.

Watch it in action