I'm making a client side React app and want to set an environment variable at runtime. My package.json
contains
"scripts": {
"start": "cross-env BUILD_TYPE=dev react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
and in my app I have
let api_base_url;
if (process.env.BUILD_TYPE === 'dev') {
api_base_url = process.env.REACT_APP_API_DEV_SERVER_URL
console.log("dev build");
} else {
api_base_url = process.env.REACT_APP_API_SERVER_URL;
console.log("not a dev build");
}
When I run my app using npm run start
, first I see cross-env BUILD_TYPE=dev react-scripts start
at the command line, then in the console I see not a dev build
and it's using my non-dev server address. What am I doing wrong?
If it matters, I'm on Debian in WSL 2 on Windows 11.