Next.js
/Beginner
Environment Variables
Definition
Values that are dynamically injected into the application at runtime or build time, keeping sensitive secrets (like API keys and Database passwords) completely out of the source code.
Explain Like I'm New
You NEVER type your Database Password directly into `db.ts`. If you commit it to GitHub, hackers will find it and delete your database in 5 minutes. Instead, you put it in a `.env` file, which is explicitly ignored by GitHub.
Real World Example
Using a `.env.development` file on your laptop to connect to a local dummy database, and using Vercel's dashboard to set the Production environment variables to connect to the real database.
Common Use Cases
- •Secret management
- •Different environments (Dev vs Prod)
- •Configuration
Interactive Example
Loading Code Playground...
Interview Questions
basic
- What prefix MUST you add to an environment variable in Next.js if you want it to be exposed to the browser (Client Components)?
intermediate
- If a variable is named `DATABASE_URL` (without the prefix), can a Client Component read it?