React build fetch requests failing

Viewed 309

Mine react apps fetch requests work fine when I test in dev environment (npm start) but after creating a build package (npm run build) and then running that, all my fetch requests receive a failed status when making requests to my locally hosted API. Could this be because my API is not https? Any other ideas?

Thanks

2 Answers

Double check if you have the REACT_APP_API_URL on your .env files:

  • .env (it should be localhost)
  • .env.production (this is used on build, it should be production URL)

Try adding a proxy field in the package.json of your react app. Once that is in place, run build and test. This usually solves the api issue that react can have with api.

package.json: proxy: "http://localhost:8000"

requests in React app: fetch('/api/urls/....')

Related