{"success":true,"name":"fail-fast-at-startup-validate-required-config-before-serving","description":"App reads env vars or config and just hopes they are all set correctly","category":"ai-brimind","path":"ai-brimind/fail-fast-at-startup-validate-required-config-before-serving/SKILL.md","file":"SKILL.md","content":"---\nname: fail-fast-at-startup-validate-required-config-before-serving\ndescription: \"App reads env vars or config and just hopes they are all set correctly\"\n---\n\n# Fail Fast at Startup: Validate Required Config Before Serving a Single Request\n\n## When to use\nThe app depends on environment variables or config values (DB connection string, API keys, secrets, base URLs), and nothing checks at startup that they exist and look sane — missing config only surfaces later, as weird failures deep inside features.\n\n## Example request\nJWT secret env var missing → the app boots fine, then every login mysteriously fails with 'undefined' baked into tokens; a startup check would have said 'JWT_SECRET is missing' in one line. DB connection string has a typo → the server starts, and the first user request explodes with a cryptic driver error; connecting (or validating) at boot turns that into an immediate, readable crash. A new feature added a new env var → it exists on your machine but nobody added it to production → the feature silently misbehaves only in prod; a single required-vars list catches the gap at deploy time. App boots, logs 'listening on port 3005' → that line proves nothing about config health; 'started' should mean 'checked and ready', not merely 'process alive'.\n\n## Instructions\nAct as an engineer who believes an app should refuse to start incomplete rather than start broken — because a crash at boot with a clear message costs one minute, while a missing config value discovered through weird feature behavior costs an afternoon of hunting in the wrong layer. For any app using env vars or config, do this: (1) Gather the app's true config dependencies into ONE visible list: every env var and setting it needs to function (DB URL, secrets, third-party keys, base URLs, port). If they are currently read ad-hoc all over the codebase with process.env sprinkled everywhere, that scattering IS the first fix: one config module reads and exports them, so the list exists in one place and the rest of the code imports from it. (2) At startup, before listening for requests, validate the list: every required value present, and cheap sanity where it matters (URL looks like a URL, port is a number). On failure, crash immediately with a message that names the exact missing variable — 'Missing required env: MONGODB_URI' — not a stack trace from three layers down. This pattern has a name, fail fast, and small validation helpers or a schema check do it in a few lines; check if the project's ecosystem already offers one before hand-rolling something elaborate. (3) Extend fail-fast to critical connections: attempt the DB connection at boot and treat failure as a startup failure with a clear log — an API server that cannot reach its database is not 'up', and pretending otherwise just moves the crash onto the first user. (4) Keep an up-to-date .env.example in the repo listing every required variable with placeholder values — it is the deployment checklist future-you and teammates will silently thank you for, and it is how 'new env var added' stops being a prod-only surprise. (5) Make the success path speak too: one startup log line confirming environment name and that config checks passed. Only consider startup handled once you can say: 'if any required config is missing or broken, the app refuses to run and names the culprit in plain words' — an app that boots happily into a broken state is not robust, it is just postponing the error to a worse time and a harder-to-read place.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #214\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}