Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A .env variable is undefined in a Create React App project

Errorprocess.env.REACT_APP_API_URL is undefined

Short 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.

  • Applies to react-scripts 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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);
  2. 2Match what you see, then jump to its fix

    1. 1Other REACT_APP_ names work, this one is undefinedThe variable name does not start with REACT_APP_Fix
    2. 2.env sits in src/ or in a parent folderThe .env file is not in the project rootFix
    3. 3The value changes only after you stop and start npm startThe dev server was not restarted after the changeFix
    4. 4The 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

What does the error look like?

  • process.env.REACT_APP_API_URL is undefined
  • React 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 '^#' .env

2Fix

Rename the variable in .env and in the code.

# .env
REACT_APP_API_URL=https://api.example.com

3Verify

Restart the dev server and log the value.

console.log(process.env.REACT_APP_API_URL);

Confirmed by create-react-app.dev

Did this fix it?

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 ./.env

3Verify

Restart and log the variable.

npm start

Confirmed by create-react-app.dev

Did this fix it?

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-scripts

2Fix

Stop the server and start it again.

npm start

3Verify

The new value is logged.

console.log(process.env.REACT_APP_API_URL);

Confirmed by create-react-app.dev

Did this fix it?

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 build

3Verify

Search the built files for the new value.

grep -r "api.example.com" build/static/js | head -n 1

Confirmed by create-react-app.dev

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.