Env Loader
Typed environment loading and validation. This page runs load(schema, { source }) on the server at build time with fixed inputs. The real environment always wins over a .env file.
.env file
# .env
NODE_ENV=development # overridden by the real environment below
PORT=3000
DEBUG=on
API_KEY="sk-live-abc123secret"
GREETING="hello\nworld"
URL=http://example.test/#anchor
parseEnvFile() — comments stripped, quotes and escapes handled
{
"NODE_ENV": "development",
"PORT": "3000",
"DEBUG": "on",
"API_KEY": "sk-live-abc123secret",
"GREETING": "hello\nworld",
"URL": "http://example.test/#anchor"
}
Real environment (wins over the file)
{
"NODE_ENV": "production",
"PORT": "8080"
}
Schema
{
"NODE_ENV": {
"type": "string",
"default": "development"
},
"PORT": {
"type": "number",
"default": 3000
},
"DEBUG": {
"type": "boolean",
"default": false
},
"API_KEY": {
"type": "string",
"secret": true
}
}
Result — NODE_ENV and PORT from the real env, the rest from the file
{
"NODE_ENV": "production",
"PORT": 8080,
"DEBUG": true,
"API_KEY": "sk-live-abc123secret"
}
JSON.stringify(env) — secret is not enumerable, so it never leaks
{"NODE_ENV":"production","PORT":8080,"DEBUG":true}
toSafeLog() — secrets masked
{
"NODE_ENV": "production",
"PORT": 8080,
"DEBUG": true,
"API_KEY": "***"
}
strict: true — real output for a .env file containing DATABSE_URL (typo)
Env validation failed:
- Unknown env var in .env file: DATABSE_URL