Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

187
Vistas
React .env files display in console but not in fetch()

I have a react app created with create-react-app, I also have a .env in the root folder with REACT_APP_SERVER_URL.

In my localhost environment, everything works as expected but when I build and deploy to Firebase then I get the error SyntaxError: Unexpected token < in JSON at position 0.

In the method, if I output it to the console console.log(process.env.REACT_APP_SERVER_URL) then the url is written to the console and I can click it and go to the home page which is just a json response (for testing).

However, in the following code, I get the SyntaxError as above

fetch(`${process.env.REACT_APP_SERVER_URL}/`, {
mode:"cors",
credentials :"include",
method : "GET",
headers : {
   Accept : "application/json",
   "Content-Type"  :"application/json"
   }
})
.then(response => response.json())
.then(data => {
     setHello(data.message)
     console.log(data.message)
})
.catch(er => {
     console.log("fetch was caught")
     console.log(er)
})

If Iconsole.log(process.env) then I can see the object with the correct value for the server url and if i replace the url in the fetch from the .env variable to hardcoded url (exact same as what gets printed to the console eg https://serverapi.com/) then everything works as expected again, and I see the data.message.

I've tried something like const {REACT_APP_SERVER_URL} = process.env but that didn't work either.

I'm running out of ideas as to why I get the error with the environment variable.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It clicked as I was typing this but in my .env file I had REACT_APP_SERVER_URL=https://server.com/ then in the fetch I had fetch(`${process.env.REACT_APP_SERVER_URL}/`) So because I had a / at the end of the SERVER_URL line and also in the fetch call then the final code had two // and looked like https://server.com// which was the issue. I fixed this by removing the / in the .env file

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda