A cli to load dotenv files
NPM
bash $ npm install -g dotenv-cli
Yarn
bash $ yarn global add dotenv-cli
$ dotenv
This will load the variables from the .env file in the current working directory and then run the command (using the new set of environment variables).
Another .env file could be specified using the -e flag:
bash $ dotenv -e .env2
Multiple .env files can be specified, and will be processed in order:
bash $ dotenv -e .env3 -e .env4
Some applications load from
.env,
.env.local,
.env.developmentand
.env.development.local(see #37 for more information).
dotenv-clisupports this using the
-cflag for just
.envand
.env.localand
-c developmentfor the ones above.
If you want to check the value of an environment variable, use the
-pflag
bash $ dotenv -p NODE_ENV
If you want to pass flags to the inner command use
--after all the flags to
dotenv-cli.
E.g. the following command without dotenv-cli:
bash mvn exec:java -Dexec.args="-g -f"
will become the following command with dotenv-cli:
bash $ dotenv -- mvn exec:java -Dexec.args="-g -f"or in case the env file is at
.my-env
bash $ dotenv -e .my-env -- mvn exec:java -Dexec.args="-g -f"
We support expanding env variables inside .env files (See dotenv-expand npm package for more information)
For example:
IP=127.0.0.1 PORT=1234 APP_URL=http://${IP}:${PORT}Using the above example
.envfile,
process.env.APP_URLwould be
http://127.0.0.1:1234.
If your
.envfile looks like:
SAY_HI=hello!
you might expect
dotenv echo "$SAY_HI"to display
hello!. In fact, this is not what happens: your shell will first interpret your command before passing it to
dotenv-cli, so if
SAY_HIenvvar is set to
"", the command will be expanded into
dotenv echo: that's why
dotenv-clicannot make the expansion you expect.
One possible way to get the desired result is:
$ dotenv -- bash -c 'echo "$SAY_HI"'
In bash, everything between
'is not interpreted but passed as is. Since
$SAY_HIis inside
''brackets, it's passed as a string literal.
Therefore,
dotenv-cliwill start a child process
bash -c 'echo "$SAY_HI"'with the env variable
SAY_HIset correctly which means bash will run
echo "$SAY_HI"in the right environment which will print correctly
hello
You can add the
--debugflag to output the
.envfiles that would be processed and exit.