I have tried to access to the environment variables in two ways and in both I've got the same result. The environment variable is "undefined".
First way
I have follow these steps:
I have installed the module "env-cmd"
I have created a root file called .env.development with this content:
BACKEND_PROTOCOL = http
BACKEND_DOMAIN = localhost
BACKEND_PORT = 4000
In the script to start my application I have modified with this commands:
"env-cmd -f .env.development react-scripts start"
console.log(${process.env.BACKEND_PROTOCOL}://${process.env.BACKEND_DOMAIN}:${process.env.BACKEND_PORT});
console.log(process.env.BACKEND_PROTOCOL);
I start the development server with:
env-cmd -f .env.development react-scripts start
I have follow this instructions: enter link description here
Second way
I have installed dotenv module
I have created in the root a file called .env
I have filled this file with these values: BACKEND_PROTOCOL = http
BACKEND_DOMAIN = localhost
BACKEND_PORT = 4000
I make an import of dotenv with:
import config from "dotenv";
Before to get access to the variables I call to config and the I try to get the values of the environment:
config.config();
console.log(${process.env.BACKEND_PROTOCOL}://${process.env.BACKEND_DOMAIN}:${process.env.BACKEND_PORT});
console.log(process.env.BACKEND_PROTOCOL);
I start my development server with this intructions:
react-scripts start
And I've got the same result. I've got undefined.
What am I doing wrong?