skip to Main Content

I’m trying to use my api key as an env variable in my react application by defining it in the .env file. However when run "npm run dev" I get the console error process is not defined from lines

main.jsx

import dotenv from 'dotenv'
dotenv.config()

and

component.jsx

  const api_url =env.REACT_APP_API_URL

Ive setup my env var as follows:

REACT_APP_API_URL = "http://127.0.0.1:5000/upload"

2

Answers


  1. use process.env.REACT_APP_API_URL, also you don’t need to import dotenv
    create-react-app provides this functionality out of the box, assuming you’re using [email protected] or higher

    Login or Signup to reply.
  2. I think you missed the word process in your component.jsx file.
    You might check this line;

      const api_url =env.REACT_APP_API_URL
    

    =>

      const api_url = process.env.REACT_APP_API_URL ||  "http://127.0.0.1:5000/upload";
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search