Azure无法在React Web App中访问环境变量

问题描述:

几年前,我看到与此类似的问题,但是超链接答案已被Microsoft关闭.

I saw there was a similar question to this a few years ago, but the hyperlink answer has been closed by microsoft.

我有一个使用create-react-app创建的react应用.它已经被部署到heroku并加载了一些环境变量以访问google maps等.这一切对于heroku都很好.

I have a react app created using create-react-app. It has been deployed already to heroku and loads in some environmental variables to access google maps etc. This all work just fine for heroku.

我已经采用了这段代码,并且几乎按照此处的说明进行操作:

I have taken this code and pretty much followed the instructions here: https://docs.microsoft.com/en-gb/azure/app-service/app-service-web-get-started-nodejs I basically uploaded a zip of my 'npm run build' using kudu.

我可以看到该Web应用程序处于在线状态,并且正在由Azure提供服务.但是,即使我可以在kudu中看到它们,我的应用程序也无法读取我在门户中设置的环境变量,从而导致其崩溃.当我尝试console.log process.env时,我得到 {NODE_ENV:"production",PUBLIC_URL:"} ,但是看不到我在kudu中看到的任何变量.

I can see that the web app is online and is being served by azure. But the environmental variables I set up in the portal are not being read by my app, causing it to crash, even though I can see them in kudu. when i try to console.log process.env I get {NODE_ENV:"production",PUBLIC_URL:""}, but see none of the varaibles that I can see in kudu.

如果我尝试记录特定变量,则会返回未定义"有人知道我在做什么错吗?

If I try to log a specific variable I get back 'undefined' Does anyone know what Im doing wrong?

在Azure内:使用 REACT_APP_MY_ENV_VAR

Within Azure: Instead of MY_ENV_VAR, use REACT_APP_MY_ENV_VAR

在React内:代替 process.env.MY_ENV_VAR ,使用 process.env.REACT_APP_MY_ENV_VAR

Within React: Instead of process.env.MY_ENV_VAR, use process.env.REACT_APP_MY_ENV_VAR

希望这会有所帮助