【问题标题】:how to use .env.qa or .env.staging with create react app如何使用 .env.qa 或 .env.staging 创建反应应用程序
【发布时间】:2020-09-06 12:26:02
【问题描述】:

CreateReactApp 文档显示您可以使用有限数量的 .env 文件。 https://create-react-app.dev/docs/adding-custom-environment-variables/#adding-development-environment-variables-in-env

我想为其他环境使用更多的 .env 文件,但始终使用 .env.development

如何使用其他文件?我找到了这篇文章,但它对我不起作用。 https://medium.com/@tacomanator/environments-with-create-react-app-7b645312c09d

我正在使用react-scripts v3.4.1

【问题讨论】:

    标签: reactjs create-react-app dotenv env-cmd


    【解决方案1】:

    嗯,这篇文章其实是对的。我的代码中有一个错误,我对值进行了硬编码,而不是依赖于process.env.REACT_APP_SOMEVAR

    package.json 中的这些脚本可以正常工作:

    "start": "REACT_APP_ENV=dev npm run start-env",
    "start-env": "sh -ac '. .env.${REACT_APP_ENV}; react-scripts start'",
    "start-dev": "REACT_APP_ENV=dev npm run start-env",
    "start-qa": "REACT_APP_ENV=qa npm run start-env",
    "build": "REACT_APP_ENV=dev npm run build-env",
    "build-env": "sh -ac '. .env.${REACT_APP_ENV}; react-scripts build'",
    "build-dev": "REACT_APP_ENV=dev npm run build-env",
    "build-qa": "REACT_APP_ENV=qa npm run build-env",
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-06
      • 2021-07-22
      • 1970-01-01
      • 1970-01-01
      • 2017-06-09
      • 2020-04-06
      • 2020-05-17
      相关资源
      最近更新 更多