Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
React: Environment variables returns undefined

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:

  1. I have installed the module "env-cmd"

  2. I have created a root file called .env.development with this content:

    BACKEND_PROTOCOL = http

    BACKEND_DOMAIN = localhost

    BACKEND_PORT = 4000

  3. In the script to start my application I have modified with this commands:

"env-cmd -f .env.development react-scripts start"

  1. I try to get the value with:

console.log(${process.env.BACKEND_PROTOCOL}://${process.env.BACKEND_DOMAIN}:${process.env.BACKEND_PORT}); console.log(process.env.BACKEND_PROTOCOL);

  1. 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

  1. I have installed dotenv module

  2. I have created in the root a file called .env

  3. I have filled this file with these values: BACKEND_PROTOCOL = http

    BACKEND_DOMAIN = localhost

    BACKEND_PORT = 4000

  4. I make an import of dotenv with:

    import config from "dotenv";

  5. 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);

  6. 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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!