Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

reactjs - How to add environment variables within the meta tags inside public/index.html in creat-react-app?

%ENV_VARIABLES%

I tried this and restarted create-react-app with npm start, its not working for me, any help would be appreciated. Thank You


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

instead of putting it inside index.html, do the following and the variables will be available inside your app (auto added to meta).

create a .env file

var=var_value

then install dotenv

npm install dotenv

put the following at the top of the file where you want to use env variable

require('dotenv').config()

You can access it like the following

process.env.var

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...