portfolio-page/backend/services/env.js

17 lines
698 B
JavaScript

/**
* Reads an environment variable, treating blank as absent.
*
* `process.env.X ?? fallback` is not enough: a key that is present but empty
* - which is how .env.example ships the optional ones, e.g. `SMTP_FROM=` -
* is an empty string, not undefined, so `??` keeps the empty value and the
* fallback never applies. That produced an empty From header and port 0.
*
* Dependency-free on purpose, so both server.js and emailService.js can use
* it without dragging nodemailer into the importing module's graph.
*/
export function env(name, fallback = undefined) {
const value = process.env[name]
if (value === undefined || value.trim() === '') return fallback
return value.trim()
}