[英]How to read environment variables in react app passed through docker-compose.yaml file during run time?
I am unable to read environment variables declared in docker-compose.yml in my react app.我无法在我的 react 应用程序中读取 docker-compose.yml 中声明的环境变量。 The app is created using create-react-app
该应用程序是使用 create-react-app 创建的
# docker-compose.yml
```
image: sample-app
container_name: sample-app
restart: always
ports:
- "80:80"
environment:
REACT_APP_FIREBASE_API_KEY: "dsdsdsdsdHUXfYg5_QpQ"
REACT_APP_FIREBASE_PROJECT_ID: "simple_project"
REACT_APP_FIREBASE_MESSAGING_SENDER_ID: "22762762"
REACT_APP_PRISMA_EXPRESS_URL: "http://trial.com:4000"
REACT_APP_PYTHON_DEVELOPMENT_URL: "http://trial:4200"
```
The below is the config.js file in my react app.以下是我的反应应用程序中的 config.js 文件。 Here I try to read the environment variable which are passed through docker-compose.yml.
在这里,我尝试读取通过 docker-compose.yml 传递的环境变量。 The actual values passed through docker-compose are not read in config.js
通过 docker-compose 传递的实际值不会在 config.js 中读取
# config.js
```
export const stored_values={
//firebase configuration
apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID,
backendURL: process.env.REACT_APP_PRISMA_EXPRESS_URL
}
```
What am I doing wrong here ?我在这里做错了什么?
I implemented it with the help of these articles我在这些文章的帮助下实现了它
https://www.freecodecamp.org/news/how-to-implement-runtime-environment-variables-with-create-react-app-docker-and-nginx-7f9d42a91d70/ https://www.freecodecamp.org/news/how-to-implement-runtime-environment-variables-with-create-react-app-docker-and-nginx-7f9d42a91d70/
How to access window variable in Javascript object? 如何访问 Javascript 对象中的窗口变量?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.