简体   繁体   English

react-create-app环境变量配置

[英]react-create-app ENVIRONMENT variable configuration

i am creating react application using create-react-app,i am using node js as a backend development. 我正在使用create-react-app创建react应用程序,我正在使用node js作为后端开发。

i can run it successfully in the local environment,but while having same application in the production build application doesn't set the "apiURL" 我可以在本地环境中成功运行它,但是在生产构建应用程序中具有相同应用程序时,不会设置“ apiURL”

my backend url is= http://localhost:5001/ 我的后端网址是= http:// localhost:5001 /

API_URL = process.env.REACT_APP_API_URL API_URL = process.env.REACT_APP_API_URL

actually i need to get my API_URL = http://localhost:5001 but i am getting API_URL is undefined in the production deploy 实际上,我需要获取我的API_URL = http:// localhost:5001,但我正在生产部署中获取未定义的API_URL

and on request of some backend api i am getting 和一些后端API的要求,我越来越

http://localhost:3000/undefined/api/someapi HTTP://本地主机:3000 /未定义/ API / someapi

but actually i need to get the http://localhost:5001/undefined/api/someapi 但实际上我需要获取http:// localhost:5001 / undefined / api / someapi

can any one help how can configure my environment variable dynamically depends on the host environment 任何人都可以帮助如何动态配置我的环境变量取决于主机环境

sorry for my bad english 对不起,我的英语不好

thanks in advance 提前致谢

You need to set different values of your variables depending on your environment. 您需要根据环境设置不同的变量值。 There're multiple possibilities, you can for example do it on script level in your package.json: 有多种可能性,例如,您可以在package.json中的脚本级别执行此操作:

"scripts": {
   "deploy:staging": "REACT_APP_API_URL=<paste-your-api-for-staging> ...",
   "deploy:production": "REACT_APP_API_URL=<paste-your-api-for-production> ...",
}

Or you can use my personal favorite -> https://github.com/motdotla/dotenv for managing your env variables. 或者,您可以使用我个人最喜欢的-> https://github.com/motdotla/dotenv来管理环境变量。 It's based on different .env files for eg. 它基于不同的.env文件,例如。 .env.local or .env.production where you can set your values. .env.local或.env.production,您可以在其中设置值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM