Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda