At the point when you don’t have a server-side programming foundation, environment variables can appear to be a touch of enchantment. That absence of information smacks you in the face when you’re finished making todo applications on your localhost and attempt to make a production build for the application.
The issue we’re illuminating:
Step by step instructions to provide API URL for your development mode and production environment or staging or QA environment.
When working with React, the environment variable is factors that are accessible through a global process.env Object. That worldwide Object is given by your condition through NodeJs. Also, in light of the fact that we don’t have NodeJs in the program, we’re going to require a webpack.
Why You Need Environment Variables
You need condition factors for two things:
- To modify factors dependent on your condition, for example, regardless of whether it is development, staging, or production, and so on.
- To store important data. Things like API keys and passwords are profoundly delicate and you shouldn’t push them with code in open.
We are going to Make React App underpins custom condition factors without the need to introduce some other bundles.
There are two techniques for including condition factors:
- Through the shell (temporary, last till the session lasts, and differs relying upon the OS type).
- Utilizing the .env file(This is the methodology we are going to utilize).
How to create a custom Environment Variable File in the project.
- Go to the root folder of your project directory.
- Now create “.development.env” file for development & “.production.env” file for production “.statging.env” for staging environment and the same goes for QA as required.
- In environment file create variable as REACT_APP_API_POINT = “http://yourcustompath.com”. Here REACT_APP_ is mandatory as it will be recognized as a global variable by ReactJS environment. You can create as many as many variables as you want in a file. Starting with REACT_APP_
REACT_APP_API_ENDPOINT = "http://188.8.131.52:8080"
REACT_APP_API_BASEURL = "/foxmatrixapp/v1/api/"
CUSTOM_NODE_ENV = "staging"
- Now to use these environment variables in React install dotenv for support of environment file in ReactJs. and install env-cmd for custom environment files Now you are ready to use these variables in React.
- In a jsx file to use these variable write process.env.REACT_APP_API_POINT. Here process.env. Is to tell react these are the global variable.
Now as you are ready to use these variables in react. We come to how to create a custom react environment for staging or QA.
- Goto your package.json file where you will find Script written for start and build these are the command used for starting the development environment and create a build for production.
- To Create your custom build add “build:staging”:”env-cmd -f .env.stating npm run build”, adding this command will start creating a build for staging environment as for now.
- You could add as many different environments as you want.
"start": "react-scripts start",
"build": "react-scripts build",
"build:staging": "env-cmd -f .env.staging npm run build",
"build:qa": "env-cmd -f .env.qa npm run build",
"test": "react-scripts test",
"eject": "react-scripts eject"
InnovationM is a globally renowned Mobile app development company in India that caters to a strong & secure Android app development, iOS app development, hybrid app development services. Our commitment & engagement towards our target gives us brighter in the world of technology and has led us to establish success stories consecutively which makes us the best Android app development company in India.
Thanks for giving your valuable time. Keep reading and keep learning.