Boneyard Tools

Dotenv Parsing Rules: Quotes, Export and Comments

How this parser handles KEY=value lines, comments, the export prefix, surrounding quotes, and values that contain an equals sign.

One line, one key, one value

The parser walks the file line by line, splitting on carriage return and line feed so Windows and Unix endings both work. Each surviving line is trimmed and then split on the first equals sign. The text before that sign, trimmed, is the key, and everything after it, trimmed, is the raw value. Splitting on the first equals rather than every equals is what lets a connection string like postgres://a=b keep its second equals sign inside the value instead of losing it.

Comments, blanks and the export prefix

Before a line is parsed, two shortcuts apply. If the trimmed line is empty or begins with a hash, it is skipped entirely, which is how comments and spacer lines disappear from the output. Next, if the line begins with the literal text export followed by a space, that prefix is stripped, so a shell-style export TOKEN=abc123 becomes a plain TOKEN=abc123 before parsing continues. This keeps files that were written to be sourced by a shell fully compatible.

Quote stripping and why types stay strings

After the value is trimmed, the parser removes exactly one pair of matching surrounding quotes, either single or double. So "My App" and 'My App' both yield My App, but a stray quote on only one side is left alone. Notice what it does not do: it never converts 3000 into a number or true into a boolean. Environment variables are strings by definition, so every value in the JSON output is quoted, matching what your code sees in process.env.

Going back the other way

The reverse direction serializes a flat JSON object into dotenv lines. Each key becomes a KEY=value line, and the value is coerced to a string first, so a null becomes an empty value. A value is wrapped in double quotes only when it needs it, meaning it contains whitespace, a hash, a quote character, or has leading or trailing spaces. When quoting is applied, embedded double quotes and backslashes are escaped so the file round-trips cleanly back to JSON.

Frequently asked questions

Does it keep multi-line or interpolated values?

No. The parser treats every physical line as its own entry and does no variable interpolation, so a value referencing another variable stays as literal text and a value cannot span multiple lines.

Why is my quoted number still a string in the JSON?

Because dotenv has no concept of number or boolean types. The tool strips surrounding quotes but always emits string values, so PORT=3000 and PORT="3000" both become the string 3000 in the JSON.

What if a value already contains double quotes?

Going from JSON to .env, inner double quotes are escaped with a backslash and the whole value is wrapped in double quotes, so it survives a later round trip back into JSON without breaking.