DotEnv.jl is a lightweight package that loads environment variables from .env
files into =ENV=. Storing configuration in the environment is based on The
Twelve-Factor App methodology.
Please don’t store secrets in dotenv files, and if you must at least ensure the
dotenv file(s) are listed in .gitignore.
using DotEnv
DotEnv.load!()Create a .env file in your project. You can add environment-specific variables
using the rule NAME=VALUE. For example:
#.env file
DB_HOST=127.0.0.1
DB_USER=john
DB_PASS=42
When DotEnv.load!() is called, all variables declared in the .env file that
are not already present in ENV are loaded into it.
julia> ENV["DB_PASS"]
"42"To load variables from dotenv files even when they are already present in the
environment dictionary, pass override = true to DotEnv.load!.
DotEnv.unload!() will reverse the changes of DotEnv.load!().
This works even when the environment is modified incrementally (i.e. loading files one at a time).
config reads a dotenv file, parse the content, applies variable expansion, but
does not modify the base environment dictionary.
julia> using DotEnv
julia> cfg = DotEnv.config() # defaults to reading `.env`
DotEnv.EnvOverlay{Base.EnvDict} with 3 entries:
"DB_PASS" => "42"
"DB_HOST" => "127.0.0.1"
"DB_USER" => "john"DotEnv.parse accepts a String or an IOBuffer (Any value that can be converted into String), and it will return
a Dict with the parsed keys and values.
julia> using DotEnv
julia> DotEnv.parse("FOO=bar\nBAR=baz")
Dict{String, String} with 2 entries:
"FOO" => "bar"
"BAR" => "baz"- Leading whitespace is ignored
- Empty lines, and lines starting with
#are skipped exportprefixes are ignored (e.g.export FOO=bar)- Empty assignments are regarded as the empty string(
EMPTY=becomes"EMPTY" => "") - Keys and values are separated by === or
:, and any amount of whitespace - All values are strings, but can be quoted with single (
') or double quotes (") - Quotes of the same type may occur within quoted values, but must be escaped with
\ - Inline comments are started with a
#outside of a quoted value - Inside double quoted values,
\nnewlines are expanded, allowing for multiline values - Extra spaces are removed from both ends of an unquoted value (e.g.
FOOsome value = becomes"FOO" => "some value") - Variable expansion occurs within unquoted and double-quoted values.
$NAME,${NAME}, and${NAME:-default}expansions are all supported. - Malformed lines are silently skipped