Challenge - 5 Problems
Next.js Env Vars Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does Next.js handle environment variables in production?
Consider a Next.js app deployed to production with environment variables defined in a .env.production file. Which statement best describes how these variables are accessed in the client-side code?
Attempts:
2 left
💡 Hint
Think about security and what Next.js exposes to the browser.
✗ Incorrect
Next.js only exposes environment variables prefixed with NEXT_PUBLIC_ to client-side code. This prevents accidental exposure of sensitive data.
📝 Syntax
intermediate1:30remaining
Correct syntax to access environment variables in Next.js server code
Which option shows the correct way to access an environment variable named API_SECRET in Next.js server-side code?
Attempts:
2 left
💡 Hint
Server code uses Node.js environment variables.
✗ Incorrect
Server-side code accesses environment variables via process.env.VARIABLE_NAME. The NEXT_PUBLIC_ prefix is for client-side only.
🔧 Debug
advanced2:30remaining
Why is my client-side environment variable undefined in production?
You have
NEXT_PUBLIC_API_URL defined in your .env.production file. In your React component, you use process.env.NEXT_PUBLIC_API_URL but it is undefined after deployment. What is the most likely cause?Attempts:
2 left
💡 Hint
Think about when Next.js loads environment variables.
✗ Incorrect
Next.js loads NEXT_PUBLIC_ environment variables at build time for client-side code. If you add or change them, you must rebuild the app to see changes after deployment.
🧠 Conceptual
advanced3:00remaining
Security implications of environment variables in Next.js production
Which statement best explains why sensitive environment variables should NOT be prefixed with NEXT_PUBLIC_ in Next.js?
Attempts:
2 left
💡 Hint
Consider what code runs in the browser.
✗ Incorrect
Any environment variable prefixed with NEXT_PUBLIC_ is included in the JavaScript sent to the browser, so sensitive data must never use this prefix.
❓ state_output
expert2:00remaining
What is the value of process.env.NODE_ENV in a Next.js production build?
After running
next build and deploying your Next.js app, what is the value of process.env.NODE_ENV during runtime in production?Attempts:
2 left
💡 Hint
NODE_ENV indicates the environment mode.
✗ Incorrect
When Next.js builds for production, it sets process.env.NODE_ENV to "production" to optimize performance and behavior.