dotenv
as devDependencies:npm i --save-dev dotenv
.env
file in the root directory:my-react-app/
|- node-modules/
|- public/
|- src/
|- .env
|- .gitignore
|- package.json
|- package.lock.json.
|- README.md
.env
file like below & REACT_APP_ is the compulsory prefix for the variable name.REACT_APP_BASE_URL=http://localhost:8000
REACT_APP_API_KEY=YOUR-API-KEY
For example, I've create a file named base.js
and update it like below:
export const BASE_URL = process.env.REACT_APP_BASE_URL;
export const API_KEY = process.env.REACT_APP_API_KEY;
process.env.REACT_APP_BASE_URL