When I'm running project with npm run start it's working fine after reloading my env.config.js giving variables undefined
I have got a problem with env-config.js file. I implemented like this below
App.js file
import "./App.css";
function App() {
const Query = window._env_.REACT_APP_QUERY;
console.log(Query);
return (
<div className="App">
<h1>hello world</h1>
</div>
);
}
export default App;
this is env-config.js file
window._env_ = {
REACT_APP_GRANT: "grate",
REACT_APP_CLIENT: "simple",
};
finally scripts in package-json file
"scripts": {
"start": "chmod +x .\\env.sh && cp env-config.js .\\public/ && .\\env.sh && react-scripts start",
"dev": "chmod +x ./env.sh && ./env.sh && cp env-config.js ./public/ && react-scripts start",
"test": "react-scripts test",
"eject": "react-scripts eject",
"build": "react-scripts build"
},
I don't know about this file it was mentioned in scripts then I copied it here env.sh
#!/bin/bash
# Recreate config file
rm -rf ./env-config.js
touch ./env-config.js
# Add assignment
echo "window._env_ = {" >> ./env-config.js
# Read each line in .env file
# Each line represents key=value pairs
while read -r line || [[ -n "$line" ]];
do
# Split env variables by character `=`
if printf '%s\n' "$line" | grep -q -e '='; then
# tollerate = surrounded by blanks: ' = '
varname=$(printf '%s\n' "$line" | sed -E 's/^([a-zA-Z_-]+) *= *(.*)$/\1/')
varvalue=$(printf '%s\n' "$line" | sed -E 's/^([a-zA-Z_-]+) *= *(.*)$/\2/')
fi
# Read value of current variable if exists as Environment variable
value=$(printf '%s\n' "${!varname}")
# Otherwise use value from .env file
[[ -z $value ]] && value=${varvalue}
# Append configuration property to JS file
echo " $varname: \"$value\"," >> ./env-config.js
done < .env.local
echo "}" >> ./env-config.js
I'm running my project with git bash terminal with npm run start command
I don't want to use .env file for some varables because I'm unable to overide with docker build command
how can we get env-config.js variables without getting undefined after reload the project.
I'm not sure about env.sh file any more I couldn't understand what they coded earlier in that even don't know use of it