【问题标题】:How to tell React which .env file to use?如何告诉 React 使用哪个 .env 文件?
【发布时间】:2022-01-22 03:29:47
【问题描述】:

我有一个使用以下命令运行的 React 应用程序:

npm run dev

这会启动应用程序并使用.env.local 文件。 我希望能够配置一个名为 .env.production 的文件,当我运行命令 npm run prod 时将使用该文件而不是 .env.local

这些是我当前的脚本:

  "scripts": {
    "dev": "react-scripts start",
    "start": "serve -s build",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

我该怎么做?

【问题讨论】:

    标签: reactjs react-native environment-variables


    【解决方案1】:

    安装npmjs.com/package/env-cmd

     "scripts": {
            "start": "react-scripts start",
            "start:local": "env-cmd -f ./.env.local react-scripts start",
            "start:Dev": "env-cmd -f ./.env.development react-scripts start",
            "start:QA": "env-cmd -f ./.env.test react-scripts start",
            "build": "react-scripts build",
            "test": "react-scripts test",
            "eject": "react-scripts eject"
          }
    

    【讨论】:

    • 谢谢!我试过了,得到了这个'env-cmd' is not recognized as an internal or external command,
    【解决方案2】:

    创建一个新的脚本密钥并使用 REACT_APP_ENV :

     "scripts": {
        "dev": "react-scripts start",
        "start": "serve -s build",
        "build": "react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject",
        "prod" : "REACT_APP_ENV=prod npm run dev"
      },
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 2020-10-13
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      相关资源
      最近更新 更多