- Environment variables in Create-react-app projects must be prefixed with
REACT_APP_
, for example:REACT_APP_API_URL = http://localhost:3001/posts/
- Another special environment variable is
NODE_ENV
, we can read it fromprocess.env.NODE_ENV
, the value will bedevelopment
,test
, orproduction
. - Environment variable is injected into code at build time.
- Access it using
process.env.REACT_APP_VAR_NAME
in JS code. - Access it in the HTML using
%REACT_APP_VAR_NAME%
.
Add environment variables in .env file
Create .env
file under the project’s root folder.
Add temporary environment variables in Shell
for example: REACT_APP_NOT_SECRET_CODE=abcdef npm start
.
Also see https://create-react-app.dev/docs/adding-custom-environment-variables/