Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Environment variables to React application from Package.json

following this article https://serverless-stack.com/chapters/environments-in-create-react-app.html

I am trying to add an environment variable to my react app which I have created using create react app. so my build command looks like below

 "build": "REACT_APP_SECRET_CODE=123 react-scripts build",

while I try to run this build command in my Visual studio code terminal via

npm run build 

it gives me the error that REACT_APP_SECRET_CODE is not recognized as an internal or external command.

How would i pass the environment variable in `package.json and build my app and access the variable value in my app.

like image 966
umer Avatar asked Jun 04 '26 14:06

umer


1 Answers

If you don't want to install any other library. You can pass environment variable to yarn or npm command like this:

"scripts": {
 "start": "breact-scripts start",
 "start:env": "REACT_APP_ABC=xyz yarn start",
}

Note: Remember to put prefix "REACT_APP_" in your environment variable otherwise React won't allow it to use in app.

like image 191
akzarma Avatar answered Jun 07 '26 22:06

akzarma