Does dotenv read YAML as env?
Yes. That is why envs has a format gate. Every row below is a measurement against dotenv 17.4.2, and you can reproduce all of them.
What dotenv accepts
| Input | dotenv 17.4.2 returns |
|---|---|
name: envsversion: 1 |
{name: "envs", version: "1"} — a YAML file, parsed
|
KEY: value |
{KEY: "value"} — colon and space work as a
separator
|
KEY:value · KEY : value |
{} — one space changes the answer |
A="oops then valid lines |
A gets the literal "oops; the rest still loads
|
{"a": 1} · - item |
{} — silently nothing |
Pointing a loader at the wrong file should fail. What you get instead is keys and values, a process that starts, and nothing anywhere that prompts someone to look.
The rule envs uses instead
An env document is a sequence of logical entries, each one of three kinds:
1. a line beginning with # a comment
2. a blank line
3. KEY=VALUE
An entry is not the same as a line. A quoted value may span several lines, so the kind is decided once the value closes. Read the rule as "every line is one of three" and it rejects a valid file with a multiline value.
A fourth kind anywhere means the file is not env format, and none of it loads. Under a lenient parser, a file with three good lines and one bad one loses the fourth without saying so.
envs validate .env
.env: ok, 12 keys
envs validate config.yaml
config.yaml:1: NOT_ENV_LINE
config.yaml:2: NOT_ENV_LINE
Findings carry a line number and a kind. Never a value. Otherwise the diagnostic output has to be handled as carefully as the file it is describing.
Values, quoting and escapes
Values may be bare or wrapped in ", ', or a
backtick. Inside quotes # and = are literal; a
bare value ends at the first #, and only bare values are
trimmed.
| Input | Result |
|---|---|
K="a\nb" · K="a\rb" |
expanded |
K="a\tb" · K="a\\b" ·
K="a\"b"
|
not expanded — the backslash stays |
K="C:\path\to" |
C:\path\to — a Windows path survives |
K='a\nb' · K=`a\nb` |
not expanded — only double quotes are special |
Only \n and \r expand, matching dotenv
exactly. JSON.parse looks like an equivalent shortcut. It
throws on "C:\path\to" and expands escapes dotenv leaves
alone, so envs does the unescaping itself.
Keys are not forced to uppercase
api_key=secret is env format. Uppercase is a convention
rather than part of the grammar, so lint mentions it and
the parser lets it through.