A .env variable is undefined in a Create React App project
process.env.REACT_APP_API_URL is undefinedShort answer
Create React App only exposes variables that start with REACT_APP_, reads them from files in the project root, loads them when the dev server starts, and bakes them into the bundle at build time. Break any of those four and the value is undefined. Fix the prefix, move the file, then restart.
Fix it fast
1Find out which cause you have
Print what the bundle actually received. Everything that does not start with REACT_APP_ is missing, apart from NODE_ENV.
console.log(process.env);2Match what you see, then jump to its fix
- 1
Other REACT_APP_ names work, this one is undefinedThe variable name does not start with REACT_APP_Fix - 2
.env sits in src/ or in a parent folderThe .env file is not in the project rootFix - 3
The value changes only after you stop and start npm startThe dev server was not restarted after the changeFix - 4
The value in the browser differs from .env, or stays old after changing it on the hostA higher-priority .env file overrides the value, or the build was made earlierFix
- 1
What does the error look like?
process.env.REACT_APP_API_URL is undefinedReact environment variable from .env returns undefined
Cause 1 of 4
The variable name does not start with REACT_APP_
1Check
Custom variables must begin with REACT_APP_. Other variables are ignored, except NODE_ENV.
grep -v '^#' .env2Fix
Rename the variable in .env and in the code.
# .env
REACT_APP_API_URL=https://api.example.com3Verify
Restart the dev server and log the value.
console.log(process.env.REACT_APP_API_URL);Confirmed by create-react-app.dev
Cause 2 of 4
The .env file is not in the project root
1Check
The file must be called .env and live in the root of the project, next to package.json.
ls -a | grep -E '^(package.json|\.env)'2Fix
Move it next to package.json.
mv src/.env ./.env3Verify
Restart and log the variable.
npm startConfirmed by create-react-app.dev
Cause 3 of 4
The dev server was not restarted after the change
1Check
Environment variables are read when the server starts. Changing one while it runs has no effect.
ps aux | grep react-scripts2Fix
Stop the server and start it again.
npm start3Verify
The new value is logged.
console.log(process.env.REACT_APP_API_URL);Confirmed by create-react-app.dev
Cause 4 of 4
A higher-priority .env file overrides the value, or the build was made earlier
1Check
For npm start the order is .env.development.local, .env.local, .env.development, .env; for npm run build it is .env.production.local, .env.local, .env.production, .env. Values are embedded during the build, not read when the page loads.
ls -a | grep '^\.env'2Fix
Put the value in the file that wins for your command, and rebuild after changing it.
npm run build3Verify
Search the built files for the new value.
grep -r "api.example.com" build/static/js | head -n 1Confirmed by create-react-app.dev
How do I stop it coming back?
- Treat every REACT_APP_ variable as public: it ends up in the shipped JavaScript.
- Commit a .env.example with the names and keep real values in .env.local.
- Rebuild and redeploy whenever a variable changes.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fix