DEV Community

Tarek Hassan
Tarek Hassan

Posted on

Adding environment variables to React

This is a quick guide to adding environment variables to React apps, so let's get to it.

Step 1, Creating .env file

Create a file in your root folder and name it .env.

Step 2, Adding variables

Inside you .env create variable like this

REACT_APP_SECRET_TOKEN=TOKEN
Enter fullscreen mode Exit fullscreen mode

Note: variables should start with REACT_APP

Step 3, Using variables inside your app

After you added .env file and variables re-build your app with npm start. Now you can use your environment variables by simply passing it inside your app {process.env.REACT_APP_SECRET_TOKEN}

Discussion (0)