Environment variables let you keep secret or changing information outside your code. In production, they help keep your app safe and flexible.
Environment variables in production in NextJS
NEXT_PUBLIC_API_URL=https://api.example.com
DATABASE_PASSWORD=supersecretpasswordVariables starting with NEXT_PUBLIC_ are exposed to the browser.
Other variables are only available on the server side.
NEXT_PUBLIC_API_URL is accessible in the browser, but DATABASE_PASSWORD is only on the server.# .env.production
NEXT_PUBLIC_API_URL=https://api.example.com
DATABASE_PASSWORD=supersecretpasswordprocess.env.NEXT_PUBLIC_API_URL.console.log(process.env.NEXT_PUBLIC_API_URL) // prints the API URL in the browser
DATABASE_PASSWORD are accessible in API routes or getServerSideProps.console.log(process.env.DATABASE_PASSWORD) // only works on server side, not in browser
This simple Next.js component shows how to display a public environment variable in the browser. The secret DATABASE_PASSWORD is not shown here and stays safe on the server.
import React from 'react' export default function Home() { return ( <main> <h1>API URL:</h1> <p>{process.env.NEXT_PUBLIC_API_URL}</p> </main> ) } // .env.production file content: // NEXT_PUBLIC_API_URL=https://api.example.com // DATABASE_PASSWORD=supersecretpassword
Always restart your Next.js server after changing environment variables.
Never expose secrets by prefixing them with NEXT_PUBLIC_.
Use .env.production for production variables and .env.local for local development.
Environment variables keep secrets and settings outside your code.
Prefix with NEXT_PUBLIC_ to expose variables to the browser.
Use different files for production and development to keep environments separate.
Practice
Solution
Step 1: Understand environment variables purpose
Environment variables store secrets and settings outside the code to keep them safe.Step 2: Identify the main use in production
In production, environment variables keep sensitive data like API keys hidden from the public.Final Answer:
To keep sensitive data like API keys out of the codebase -> Option AQuick Check:
Environment variables = keep secrets safe [OK]
- Thinking env vars speed up the app
- Using env vars for UI changes only
- Storing user data in env vars
Solution
Step 1: Recall public env var naming rule
Next.js requires public env vars to start with NEXT_PUBLIC_ to expose them to the browser.Step 2: Match the correct syntax
Only process.env.NEXT_PUBLIC_API_KEY follows this rule and is accessible in browser code.Final Answer:
process.env.NEXT_PUBLIC_API_KEY -> Option CQuick Check:
Public env vars start with NEXT_PUBLIC_ [OK]
- Using env vars without NEXT_PUBLIC_ prefix
- Assuming all env vars are accessible in browser
- Confusing NEXT_PUBLIC_ with PUBLIC_ prefix
console.log(process.env.NEXT_PUBLIC_API_URL);
And the environment variable
NEXT_PUBLIC_API_URL=https://api.example.com is set, what will be logged?Solution
Step 1: Check variable prefix and usage
The variable starts with NEXT_PUBLIC_, so it is exposed to browser code and accessible via process.env.Step 2: Confirm value assignment
The variable is set to https://api.example.com, so logging it prints this string.Final Answer:
https://api.example.com -> Option AQuick Check:
Correct prefix + set value = logged value [OK]
- Expecting undefined for public env vars
- Confusing variable name casing
- Assuming error if variable exists
process.env.NEXT_PUBLIC_API_URL logs as undefined in production. What is the most likely cause?Solution
Step 1: Understand environment variable loading in production
Next.js reads env vars from the production environment, so if the variable is missing there, it will be undefined.Step 2: Check common causes of undefined
If the variable is not set on the production server, the code logs undefined even if the code and prefix are correct.Final Answer:
The environment variable was not set on the production server -> Option DQuick Check:
Missing env var in production = undefined [OK]
- Assuming code typo without checking env setup
- Ignoring missing variable on server
- Confusing local and production env files
Solution
Step 1: Understand server-only environment variables
Variables without NEXT_PUBLIC_ prefix are only available on the server side, keeping secrets safe.Step 2: Apply correct storage and usage
Defining the key in .env.production without NEXT_PUBLIC_ and accessing it only in server code ensures it is not exposed to the browser.Final Answer:
Define the key in .env.production without NEXT_PUBLIC_ prefix and access it only in server code -> Option BQuick Check:
Server-only vars = no NEXT_PUBLIC_ prefix [OK]
- Exposing secrets by adding NEXT_PUBLIC_ prefix
- Storing secrets in public folder
- Hardcoding secrets in client code
Ready to prove it?
Pass the test and this pattern moves unknown
