DEV Community

Cover image for How To use .env in Reactjs Vite
Ayoub
Ayoub

Posted on • Updated on

How To use .env in Reactjs Vite

To use .env in react :

1.You need to create .env file .

2.Open the .env file and declare your environment variable.

VITE_REACT_APP_NAME=react app
VITE_REACT_APP_CLIENT_ID=adkksclsc.cscd52

Enter fullscreen mode Exit fullscreen mode

Note: React enforces the prefix REACT_APP on every custom variable. Variable without REACT_APP is ignored. So always create a custom variable like REACT_APP_CUSTOM_VARIABLE.
But if you are using vite you need to add the prefix vite .

3.Now to print your environment variable you need to put process.env before your custom variable.

{process.env.REACT_APP_NAME}
Enter fullscreen mode Exit fullscreen mode

4.Now start your application npm start and refresh the browser to see the changes.

Top comments (0)