Quick answer
Keys in .env files must follow a strict format: start with a letter (A-Z, a-z) or underscore (_), then only letters, digits, and underscores.
.env Invalid Key
Keys in .env files must follow a strict format: start with a letter (A-Z, a-z) or underscore (_), then only letters, digits, and underscores. Keys starting with a number or containing hyphens, spaces, or other characters are invalid.
Common causes
- Key starting with a number (e.g. 1ST_KEY=value).
- Key containing a hyphen (e.g. my-key=value).
- Key with spaces (e.g. my key=value).
How to fix
- Use only letters, digits, and underscores in key names.
- Start the key with a letter or underscore (e.g. MY_KEY, _PRIVATE).
- Replace hyphens with underscores (e.g. my_key instead of my-key).
- Validate with Env File Validator to get the exact line.
Examples
Bad
my-key=value
Good
MY_KEY=value
FAQ
- Can I use hyphens in .env keys?
- Many loaders allow it, but strict validators and some runtimes expect only [A-Za-z_][A-Za-z0-9_]*. Use underscores for portability.
Fix it now
Try in validator (prefill this example)