-1

I created environment variables in a .env file for my React project.

I have added the .env in my .gitignore file.

How do I read these environment variables from my Amazon S3 bucket since my React app is hosted there?

e.g. process.env.REACT_APP_MY_VARIABLE

Ermiya Eskandary
  • 9,664
  • 3
  • 14
  • 25

1 Answers1

0

1 - Make sure your .env file is in the root folder of your project. It should be something like:

REACT_APP_MY_VARIABLE = 'my-app-var'

2 - Build your project before sending it to S3 and install the dotenv dependency.

npm install dotenv --save

3 - Import the dotenv in your project.

Juan Fontes
  • 557
  • 2
  • 15
  • Since the .env file is not deployed to S3, how will process.env.REACT_APP_MY_VARIABLE get the value that I want? – Chris Okebata Oct 12 '21 at 09:39
  • Because of that I said to use dotenv, which allows your project to read .env files. require('dotenv/config'); console.log(process.env. REACT_APP_MY_VARIABLE) – Juan Fontes Oct 12 '21 at 10:50
  • my gitignore contains the build folder, which means the build folder is not deployed along with the code. How will dotenv know where to check for the variables in S3? – Chris Okebata Oct 12 '21 at 11:17
  • the build folder is exclude in git because the project is built in a deployment pipeline – Chris Okebata Oct 12 '21 at 11:36