Add runtime environment variable validation with fail-fast startup behavior.#44
Add runtime environment variable validation with fail-fast startup behavior.#44Koder-Kartik wants to merge 1 commit intofuzziecoder:mainfrom
Conversation
|
@Koder-Kartik is attempting to deploy a commit to the Revon Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughThis PR implements fail-fast environment validation. A new backend/env.js module loads and validates required variables (VITE_SUPABASE_URL as valid URL, VITE_SUPABASE_ANON_KEY with minimum length 10) using Zod, terminating startup with detailed errors if validation fails. The server imports this module at initialization, and services/supabase.ts transitions from combined warnings to separate error guards. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Tip Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
services/supabase.ts (1)
7-16: Clarify the error message for non-local environments.The current text suggests creating
.env.local, which is accurate for local dev but misleading in deployed environments. Consider wording that covers both.💡 Suggested wording
- "Missing environment variable: VITE_SUPABASE_URL. Create a .env.local file." + "Missing environment variable: VITE_SUPABASE_URL. Set it in the environment or .env.local." ... - "Missing environment variable: VITE_SUPABASE_ANON_KEY. Create a .env.local file." + "Missing environment variable: VITE_SUPABASE_ANON_KEY. Set it in the environment or .env.local."🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@services/supabase.ts` around lines 7 - 16, Update the thrown Error messages for the missing variables supabaseUrl and supabaseAnonKey so they don’t assume a local .env.local file; change the text to mention the missing environment variable name and provide neutral guidance such as “set this variable in your environment or deployment configuration” (or similar) so it applies to both local and deployed environments; ensure you edit the error strings in the blocks that check supabaseUrl and supabaseAnonKey.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@backend/env.js`:
- Around line 6-10: The project is missing the zod dependency and env validation
currently allows PORT as a string which can produce NaN at runtime; add "zod" to
package.json dependencies and update the env validation (the envSchema object)
to validate/coerce PORT to a number (or use z.preprocess/coerce to ensure PORT
is parsed as a number) so the app fails fast with a clear error instead of
starting with NaN when PORT is invalid.
In `@backend/server.js`:
- Line 4: The current call to require("./env") in server.js will fail under ESM;
replace it with an ES module import by using a top-level import of the env
module (ensure the filename/extension matches what's on disk, e.g., "./env.js"
if necessary) or use a dynamic import() if you need conditional loading; update
the reference to require("./env") in server.js so the environment variables are
loaded via an ES module import instead of require.
---
Nitpick comments:
In `@services/supabase.ts`:
- Around line 7-16: Update the thrown Error messages for the missing variables
supabaseUrl and supabaseAnonKey so they don’t assume a local .env.local file;
change the text to mention the missing environment variable name and provide
neutral guidance such as “set this variable in your environment or deployment
configuration” (or similar) so it applies to both local and deployed
environments; ensure you edit the error strings in the blocks that check
supabaseUrl and supabaseAnonKey.
ℹ️ Review info
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Disabled knowledge base sources:
- Linear integration is disabled
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (4)
README.mdbackend/env.jsbackend/server.jsservices/supabase.ts
| const envSchema = z.object({ | ||
| VITE_SUPABASE_URL: z.string().url(), | ||
| VITE_SUPABASE_ANON_KEY: z.string().min(10), | ||
| PORT: z.string().optional() | ||
| }); |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
cat -n backend/env.jsRepository: fuzziecoder/Brocode-Party-Update-App
Length of output: 792
🏁 Script executed:
rg -n '"zod"' package.json package-lock.json pnpm-lock.yaml yarn.lock 2>/dev/null | head -20Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 62
🏁 Script executed:
rg -n 'PORT' backend/ --type js --type ts -A 2 -B 2Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 551
🏁 Script executed:
find . -name "package.json" -type f | head -5 | xargs -I {} sh -c 'echo "=== {} ===" && cat {}'Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 935
🏁 Script executed:
rg -n 'z\.coerce' --type js --type tsRepository: fuzziecoder/Brocode-Party-Update-App
Length of output: 62
🏁 Script executed:
find . -name "package-lock.json" -o -name "pnpm-lock.yaml" -o -name "yarn.lock" | head -1 | xargs -I {} sh -c 'echo "Found: {}" && grep -A 5 '"'"'"zod"'"'"' {} | head -30'Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 106
🏁 Script executed:
rg -n 'require.*zod|import.*zod' --type js --type tsRepository: fuzziecoder/Brocode-Party-Update-App
Length of output: 126
🏁 Script executed:
ls -la package-lock.json | head -5 && grep '"zod"' package-lock.json | head -10Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 147
🏁 Script executed:
find . -path ./node_modules -prune -o -name "*.json" -type f -exec grep -l '"zod"' {} \;Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 62
🏁 Script executed:
cat package-lock.json | head -50Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 1706
🏁 Script executed:
rg -n 'zod' backend/ frontend/ src/ --type js --type ts 2>/dev/null | head -20Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 126
Add zod to dependencies in package.json — backend/env.js will fail at runtime without it.
The environment validation module requires zod but it's not listed in package.json. Once added, consider validating PORT as a number to prevent the server from attempting to listen on NaN if an invalid value is provided:
const envSchema = z.object({
VITE_SUPABASE_URL: z.string().url(),
VITE_SUPABASE_ANON_KEY: z.string().min(10),
- PORT: z.string().optional()
+ PORT: z.coerce.number().int().positive().optional()
});This ensures fail-fast validation with a clear error message instead of silent NaN at startup.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const envSchema = z.object({ | |
| VITE_SUPABASE_URL: z.string().url(), | |
| VITE_SUPABASE_ANON_KEY: z.string().min(10), | |
| PORT: z.string().optional() | |
| }); | |
| const envSchema = z.object({ | |
| VITE_SUPABASE_URL: z.string().url(), | |
| VITE_SUPABASE_ANON_KEY: z.string().min(10), | |
| PORT: z.coerce.number().int().positive().optional() | |
| }); |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@backend/env.js` around lines 6 - 10, The project is missing the zod
dependency and env validation currently allows PORT as a string which can
produce NaN at runtime; add "zod" to package.json dependencies and update the
env validation (the envSchema object) to validate/coerce PORT to a number (or
use z.preprocess/coerce to ensure PORT is parsed as a number) so the app fails
fast with a clear error instead of starting with NaN when PORT is invalid.
| import { createServer } from 'node:http'; | ||
| import { URL } from 'node:url'; | ||
| import { database, dbPath } from './db.js'; | ||
| require("./env"); |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
# Check if backend/server.js exists and read its content
head -20 backend/server.jsRepository: fuzziecoder/Brocode-Party-Update-App
Length of output: 942
🏁 Script executed:
# Check package.json for module type configuration
cat package.json | grep -A 5 -B 5 '"type"'Repository: fuzziecoder/Brocode-Party-Update-App
Length of output: 296
🏁 Script executed:
# Verify what ./env refers to (file or directory)
ls -la backend/ | grep envRepository: fuzziecoder/Brocode-Party-Update-App
Length of output: 137
🏁 Script executed:
# Check if there are ESM imports in backend/server.js
rg -n "^import\s|from\s" backend/server.jsRepository: fuzziecoder/Brocode-Party-Update-App
Length of output: 203
require("./env") will break in ESM modules.
This file uses ES module imports and is configured as ESM ("type": "module" in package.json), so require() will throw "require is not defined" at runtime. Use an ESM import statement instead.
🔧 Suggested fix
-require("./env");
+import './env.js';🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@backend/server.js` at line 4, The current call to require("./env") in
server.js will fail under ESM; replace it with an ES module import by using a
top-level import of the env module (ensure the filename/extension matches what's
on disk, e.g., "./env.js" if necessary) or use a dynamic import() if you need
conditional loading; update the reference to require("./env") in server.js so
the environment variables are loaded via an ES module import instead of require.
📋 Description
Add runtime environment variable validation with fail-fast startup behavior.
Previously, missing or invalid environment variables were not checked during startup, which could cause silent runtime errors or blank screens. This update introduces validation to ensure required variables exist before the app initializes.
🔧 Changes Made
.env.exampleto reflect required configuration.✅ Acceptance Criteria
🧪 How to Test
VITE_SUPABASE_URLfrom.env.local.npm run dev.Missing environment variable: VITE_SUPABASE_URL📸 Notes
No functional changes to features — only startup validation and developer experience improvements.
closes #25
Summary by CodeRabbit
New Features
Documentation