Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use .env file in a react js with typescript project?

I have a react js project , but with typescript. I understand we can create .env file and have some configuration defined as such,

.env file

REACT_APP_SOME_CONFIGURATION = "some value" 

and use it in the code , without importing anything , like below

const configValue = process.env.REACT_APP_SOME_CONFIGURATION 

I tried this set up in my project , and it didn't work. is it because it is typescript? how to use .env file in such scenario.

like image 690
arve Avatar asked Dec 06 '25 19:12

arve


1 Answers

In TypeScript, you need to set the return value so if this string did so

const configValue : string = process.env.REACT_APP_SOME_CONFIGURATION 

OR

const configValue: string = (process.env.REACT_APP_SOME_CONFIGURATION as string);

like image 124
Yoel Avatar answered Dec 08 '25 13:12

Yoel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!