xxxxxxxxxx
The variable in the .env file needs to start with REACT_APP_ for it to be picked up
REACT_APP_API_URL=http://localhost:3000
xxxxxxxxxx
//install dotenv via npm
//npm install dotenv --save
//At the top of your file add
require('dotenv').config();
xxxxxxxxxx
# With create react app, you need to prefix REACT_APP_ to the variable name. ex:
REACT_APP_SOME_API_KEY=some_api_key
REACT_APP_SOME_ENV_VAR=some_env_value
xxxxxxxxxx
REACT_APP_API_URL=http://localhost:3000/api
REACT_APP_CALLBACK_URL=http://localhost:3005/callback
xxxxxxxxxx
// Troubleshooting steps:
// 1. Make sure you are running your code in an environment that supports `process.env`, like Node.js.
// 2. Confirm that you have defined the environment variable correctly.
// Example code accessing and using environment variables:
// Let's say you have an environment variable named "API_KEY" with the value "123456".
// Accessing a single environment variable:
const apiKey = process.env.API_KEY;
// Using the environment variable:
if (apiKey) {
console.log(`API key: ${apiKey}`);
} else {
console.log('API key is not defined.');
}
// If the environment variable is not defined, the console output will be:
// API key is not defined.