0
0
NextJSframework~20 mins

Environment variables in production in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Env Vars Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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?
AOnly variables prefixed with NEXT_PUBLIC_ are exposed to client-side code.
BAll environment variables from .env.production are automatically available on the client side.
CEnvironment variables must be imported from the .env.production file directly in client components.
DClient-side code cannot access any environment variables in Next.js.
Attempts:
2 left
💡 Hint
Think about security and what Next.js exposes to the browser.
📝 Syntax
intermediate
1: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?
Aconst secret = window.env.API_SECRET;
Bconst secret = import.meta.env.API_SECRET;
Cconst secret = process.env.NEXT_PUBLIC_API_SECRET;
Dconst secret = process.env.API_SECRET;
Attempts:
2 left
💡 Hint
Server code uses Node.js environment variables.
🔧 Debug
advanced
2: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?
AEnvironment variables cannot be used in React components.
BYou need to import the .env.production file manually in your component.
CYou forgot to rebuild the Next.js app after adding the variable.
DYou must prefix the variable with REACT_APP_ instead of NEXT_PUBLIC_.
Attempts:
2 left
💡 Hint
Think about when Next.js loads environment variables.
🧠 Conceptual
advanced
3: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?
APrefixing with NEXT_PUBLIC_ hides variables from the client but exposes them in server logs.
BVariables prefixed with NEXT_PUBLIC_ are embedded in the client-side JavaScript and visible to anyone using the app.
CNEXT_PUBLIC_ variables are only accessible on the server and never sent to the client.
DNEXT_PUBLIC_ variables are encrypted automatically by Next.js.
Attempts:
2 left
💡 Hint
Consider what code runs in the browser.
state_output
expert
2: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?
A"production"
Bundefined
C"development"
D"test"
Attempts:
2 left
💡 Hint
NODE_ENV indicates the environment mode.