如何使用带有自定义环境变量的 create-react-app build?

How to use create-react-app build with custom enviroment variables?

我有一些自定义环境变量,例如: REACT_APP_API_URL='http://localhost:6000/api'

...和不同的环境(developtestuatproduction)。

如何为每个环境定义自定义变量和自定义构建命令

我找到了 this CRA documentation 但它适用于 Node 变量。

您可以安装 cross-env 并使用它来为您的脚本设置自定义 REACT_APP_* 环境变量:

"scripts": {
  "develop": "cross-env REACT_APP_TEST=start react-scripts start",
  "test": "cross-env REACT_APP_TEST=test react-scripts start",
  "uat": "cross-env REACT_APP_TEST=uat react-scripts start",
  "production": "cross-env REACT_APP_TEST=production react-scripts build",
}