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

95
Visualizações
REACT_APP_KEY from .env in root not being accessed

I am trying to put my API key in my .env file that is located in the root of my project( on the same level as the package json file)

When I console log the REACT_API_KEY it is read as undefined

I have restarted the server multiple times, everything is saved.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I recommend using a library called dotenv. This will read your .env file.

let's say you have an environment variable: REACT_API_KEY=APIKEY.

You can access the value like this:

require('dotenv').config();
console.log(process.env.REACT_API_KEY);

This will log: APIKEY

This will be on the backend side, you then need to use some form of node.js library to serve the data to the frontend. Here is an example using express:

const express = require('express');
const app = express();
require('dotenv').config();


app.get('/apikey', (req, res) => {
  res.send(process.env.REACT_API_KEY);
})

Now in the frontend you will need to create a GET request to this uri to actually get the data. Here is an example using axios:

axios.get('/apikey').then((res) => {
  console.log(res);
  // res will be your API key in a String format
})

Obviously, because this key is hidden in the backend, you won't want users to be able to access this endpoint in your API. you can configure which endpoints you want your users to be able to access using a webserver like nginx.

This may seem like a lot for an environment variable, although these steps will solve many of your problems in the future, for things such as user logins and registry, and other data.

This is how you do it without the ReactJS build process. If you are using create-react-app to build your app, you can use React environment variables that are embedded during the build process. Check this out in the react documentation

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to append process.env at the beginning.

about 4 years ago · Juan Pablo Isaza Relatório
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