Create React App: using environment variables in index.html

后端 未结 4 1216
予麋鹿
予麋鹿 2021-02-03 18:00

Is there a way to inject environment variables, e.g. REACT_APP_MY_API into the index.html file?

According to this, it can be done, but I can\'

4条回答
  •  故里飘歌
    2021-02-03 18:25

    From the docs (here):

    • make sure your react-scripts dependency version is equal or greater than 0.0.9
    • create a .env file at the same level of your package.json
    • make sure all you variables start with REACT_APP_; for example REACT_APP_TITLE
    • use them in you js / ts code using process.env.REACT_APP_XXX
    • or in you html code surrounding it with % ie %REACT_APP_TITLE%
    • restart your development server / re-compile your app when making changes to your .env file

提交回复
热议问题