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.