ModernCalcs

Env File Parser

Parse a .env file into a clean key-value table, catch duplicate keys, and export as JSON or JS.

5 variables · 2 comments · 2 blank lines
Duplicate key: DEBUG — the last occurrence wins in most .env loaders.
DATABASE_URLline 2
postgres://user:pass@localhost:5432/app
NODE_ENVline 3
production
API_KEYline 6
sk_live_12345
DEBUGline 7
true
DEBUGline 8
false

Env File Parser: Turn .env Files into JSON or JavaScript

A .env file looks simple, but quoted values, inline comments, and the occasional duplicate key can make it hard to know exactly what your app will load. This parser reads your .env content the way a typical dotenv loader would, shows every resolved key-value pair, flags duplicates, and exports the result as JSON or a JavaScript config object.

Formula
KEY=value / export KEY="quoted value"

Comments start with #, quoted values support \n and \" escapes.

Why Duplicate Keys Are Dangerous

It's easy to accidentally define the same variable twice in a long .env file — once at the top during initial setup, and again further down when adding a new feature. Most loaders silently use the last value, which means the earlier definition is dead code that can confuse anyone debugging the config later.

Quoted vs. Unquoted Values

Unquoted values are taken as-is up to a trailing inline comment. Quoted values (single or double quotes) preserve internal spaces and, for double quotes, support escape sequences like \n for a newline — useful for multi-line values like private keys.

From .env to Application Config

Once parsed, exporting to JSON or a JavaScript module.exports object makes it easy to feed the same values into a config validation schema, a Docker Compose environment block, or a CI secrets step without manually retyping every key.

Practical Examples

Spotting a Silent Override

Two DEBUG lines in the same file.

  • 1.Line 6: DEBUG=true
  • 2.Line 7: DEBUG=false
  • 3.Flagged as duplicate — DEBUG resolves to "false"

Exporting for a Config Schema

Turning parsed variables into JSON.

  • 1.Paste .env content
  • 2.Click Copy as JSON
  • 3.Paste into a Zod/Joi env schema validator

What Gets Parsed Correctly

  • export KEY=value: export prefix is stripped
  • KEY="value with spaces": quotes preserved and removed
  • # Comment lines: ignored
  • KEY=value # inline comment: comment stripped for unquoted values

Good Practices for .env Files

  • Keep secrets out of version control (.gitignore your .env)
  • Avoid duplicate keys — they hide real values silently
  • Use a .env.example with placeholder values for onboarding
  • Quote any value containing spaces or special characters

Frequently Asked Questions

What .env syntax does this parser support?

Standard KEY=value lines, optional 'export ' prefixes, single and double-quoted values, comment lines starting with #, and blank lines. Double-quoted values also unescape \n and \" sequences.

What happens with duplicate keys?

The parser flags every duplicate key it finds. Most .env loaders (like dotenv) use whichever occurrence comes last in the file, so a duplicate is often an accidental override you'll want to clean up.

Does this tool send my .env file anywhere?

No. Parsing happens entirely in your browser's JavaScript engine. Since .env files often contain real secrets, nothing you paste here is ever transmitted or stored.

Can I convert my .env to a JavaScript config object?

Yes. The 'Copy as JS' button generates a module.exports object with the same keys and values, ready to paste into a config file.

How are inline comments handled?

For unquoted values, anything after a ' #' (space then hash) is treated as an inline comment and stripped. Quoted values are taken literally, so a '#' inside quotes is preserved.

Why would I need this instead of just reading the file?

Large .env files accumulated over time often have duplicate or shadowed keys that are easy to miss by eye. This tool surfaces the actual parsed values and duplicates immediately.