The .env
File Format
godotenv
supports a flexible format for .env
files, closely matching the behavior of popular dotenv
libraries in other languages. This page details the supported syntax.
Basic Key-Value Pairs
The core of the format is simple KEY=VALUE
pairs.
KEY=VALUE
ANOTHER_KEY=some_other_value
Comments
Lines beginning with #
are treated as comments and are ignored.
# This is a comment
DB_HOST=localhost # Comments at the end of a line are also supported
Whitespace
Whitespace around keys and unquoted values is trimmed.
# Both of these set the key 'KEY' to 'value'
KEY = value
KEY=value
Internal whitespace in unquoted values is preserved:
# Sets KEY to 'value one two'
KEY=value one two
Quoting
Values can be enclosed in single or double quotes, which is useful for values that contain whitespace or special characters.
Single Quotes ('
)
Values within single quotes are treated literally. No character escaping or variable expansion occurs.
SINGLE_QUOTED='This is a $LITERAL value with \n'
# SINGLE_QUOTED becomes 'This is a $LITERAL value with \n'
Double Quotes ("
)
Values within double quotes support variable expansion and escape sequences like \n
(newline).
DOUBLE_QUOTED="Hello\nWorld"
# DOUBLE_QUOTED becomes "Hello" followed by a newline and then "World"
ESCAPED_QUOTE="This is an escaped \"quote\""
# ESCAPED_QUOTE becomes 'This is an escaped "quote"'
Multiline Values
Multiline values can be defined by including newlines directly within quoted strings.
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
... more key data here ...
-----END RSA PRIVATE KEY-----"
Variable Expansion (Substitution)
godotenv
supports expanding variables within values. It replaces $VAR
or ${VAR}
with the value of that variable, looking first at other variables defined in the .env
file and then in the host machine's environment.
# .env file
DOMAIN=example.com
EMAIL=user@${DOMAIN}
# From host environment (e.g., export HOME=/home/user)
APP_PATH=${HOME}/app
# Undefined variables are expanded to an empty string
UNDEFINED=${NON_EXISTENT_VAR}/path
Given the file above:
EMAIL
will beuser@example.com
.APP_PATH
will be/home/user/app
.UNDEFINED
will be/path
.
To use a literal $
sign, escape it with a backslash \$
.
The export
Prefix
The export
keyword at the beginning of a line is supported for compatibility and is ignored during parsing.
export API_KEY=12345
# This is treated identically to API_KEY=12345
YAML-style Syntax
For convenience, a colon (:
) can be used instead of an equals sign (=
) as the separator, similar to YAML syntax.
# These are equivalent
API_KEY: 12345
API_KEY=12345