简体   繁体   English

在 Azure 中部署的 React 应用程序中未定义 process.env

[英]process.env undefined in deployed React app in Azure

I have a Node app with Express for the Back-end and React for the Front-end.我有一个 Node 应用程序,后端使用 Express,前端使用 React。

Locally, I have a.env file where the env variables are located.在本地,我有一个环境变量所在的 .env 文件。 For Azure App Service, I just save the variables as a configuration so Express can get the env variables.对于 Azure 应用服务,我只是将变量保存为配置,以便 Express 可以获取环境变量。 However, React can't see the variables and it's undefined since there is no.env file in Azure DevOps.但是,React 看不到变量并且它是未定义的,因为 Azure DevOps 中没有 .env 文件。

I tried the following already:我已经尝试了以下方法:

  • Set as REACT_APP_VARIABLE the saved variables for React in the App Service config在应用服务配置中将 React 保存的变量设置为 REACT_APP_VARIABLE
  • In the Azure Build Pipeline, I've set a build variable and set $env:VARIABLE using PowerShell Task在 Azure 构建管道中,我设置了一个构建变量并使用 PowerShell 任务设置了 $env:VARIABLE
  • Updated webpack to use dotenv-webpack plugin更新 webpack 以使用 dotenv-webpack 插件

All the approach didn't work.所有的方法都没有奏效。 How do I do this?我该怎么做呢?

As a workaround, we could create .env file and configure the file content via power shell script.作为一种解决方法,我们可以创建.env文件并通过电源 shell 脚本配置文件内容。

Steps: set the variable under pipeline Variables page->add task power shell and enter the below script to create .env file and set the content.步骤:在管道变量页面设置变量->添加任务电源shell并输入以下脚本创建.env文件并设置内容。

 New-Item -Path $(System.DefaultWorkingDirectory) -Name "env.json" -Force -Value @'
 {
  "REACT_APP_SOMW_KEY": "$(REACT_APP_SOMW_KEY)",
 }
 '@
 Get-Content -Path $(System.DefaultWorkingDirectory)\env.json

We could also check this thread .我们也可以检查这个线程

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

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