PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 21, 2022

[FIXED] How to use environment variables in React with vite?

 August 21, 2022     environment-variables, javascript, reactjs, vite     No comments   

Issue

I was trying to access environment variables, at first, I got an error saying process is not defined.

Then I tried this but I'm not sure why it's not working

.env

VITE_BASE_URL="https://jikan1.p.rapidapi.com/"
VITE_HOST="jikan1.p.rapidapi.com"
VITE_KEY="abcdefghij12345678"

Page.jsx

let api = {
  base: import.meta.env.VITE_BASE_URL,
  host: import.meta.env.VITE_HOST,
  key: import.meta.env.VITE_KEY
};

function Upcoming() {
      

    console.log(api);
    return (
      <div>
      <p>{api.host}</p>
      <p>{api.base}</p>
      <p>{api.key}</p>
    </div>
)
}

I only get api.host but the base and key are undefined

can someone tell me what I'm doing wrong here?


Solution

I figured out why it's not working, silly mistake.

it's just because I didn't restart the server there is nothing wrong other than that.

if you don't stop the server and start it again,env file won't be updated.



Answered By - Dhaifallah
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing