我有一个运行以下命令的json包文件。我可以成功使用这些环境变量,但是它会使命令很长,理想情况下,如果我改为通过config加载文件,则效果会更好。有没有办法从文件而不是从下面加载环境变量?

"scripts": {
    "start:staging": "REACT_APP_ENV=staging ENV_2=some_env_variable_2_for_staging ENV_3=some_env_variable_3_for_staging react-scripts start",
    "start:production": "REACT_APP_ENV=staging ENV_2=some_env_variable_2_for_production ENV_3=some_env_variable_3_for_production react-scripts start"
  },

最佳答案

您可以相对于package.json创建一个名称为.env的文件,并将变量放入其中。

REACT_APP_ENV_test=test
REACT_APP_ENV_test2=test2

确保在所有变量前面加上REACT_APP_

10-06 08:24