6

dotenv模块应该将我的文件优先.env.local于我的.env文件,但事实并非如此。当我REACT_APP_API_BASE在两个文件中都进行了设置时,应用程序总是使用.env. .env.local如果我删除 中的匹配定义,它只会使用中的值.env


.env

REACT_APP_API_BASE = 'https://api-staging.mysite.com/api'

.env.local

REACT_APP_API_BASE = 'https://api-qa.mysite.com/api'

该应用程序正在对https://api-staging.mysite.com/api/endpoint进行 API 调用。

我究竟做错了什么?

作为参考,我没有react-scripts直接运行,我使用的是 CRACO。

包.json

  "scripts": {
    "start": "craco start",
    "build": "craco build"
  },
  "dependencies": {
    "@craco/craco": "^6.0.0",
    "@material-ui/core": "^4.11.2",
    "@material-ui/icons": "^4.11.2",
    "@material-ui/lab": "^4.0.0-alpha.57",
    "@sentry/react": "^6.2.2",
    "@sentry/tracing": "^6.2.2",
    "@sentry/webpack-plugin": "^1.14.0",
    "@stripe/react-stripe-js": "^1.3.0",
    "@stripe/stripe-js": "^1.13.0",
    "mixpanel-browser": "^2.41.0",
    "react": "^16.6.0",
    "react-dom": "^16.0.0",
    "react-gtm-module": "^2.0.11",
    "react-intl": "^5.10.14",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.1",
    "typescript": "^4.1.3",
    "universal-cookie": "^4.0.4",
    "web-vitals": "^0.2.4"
  },
  "devDependencies": {
    "@formatjs/cli": "^3.0.1",
    "@storybook/addon-a11y": "^6.1.20",
    "@storybook/addon-actions": "^6.1.20",
    "@storybook/addon-essentials": "^6.1.20",
    "@storybook/addon-links": "^6.1.20",
    "@storybook/components": "^6.1.20",
    "@storybook/node-logger": "^6.1.20",
    "@storybook/preset-create-react-app": "^3.1.5",
    "@storybook/react": "^6.1.18",
    "@testing-library/jest-dom": "^5.11.9",
    "@testing-library/react": "^11.2.5",
    "@testing-library/user-event": "^12.8.3",
    "@types/jest": "^26.0.20",
    "@types/mixpanel-browser": "^2.35.6",
    "@types/node": "^14.14.20",
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "@types/react-gtm-module": "^2.0.0",
    "@types/react-router-dom": "^5.1.7",
    "babel-plugin-formatjs": "^9.0.1",
    "babel-plugin-import": "^1.13.3",
    "dotenv-webpack": "^1.8.0",
    "eslint-plugin-sonarjs": "^0.5.0",
    "hint": "^6.1.1",
    "jest-chain": "^1.1.5",
    "react-test-renderer": "^17.0.1"
  },
  "babel": {
    "presets": [
      "react-app"
    ],
    "plugins": [
      [
        "formatjs",
        {
          "idInterpolationPattern": "[sha512:contenthash:base64:6]",
          "ast": true
        }
      ]
    ]
  },
4

1 回答 1

10

发布赏金五分钟后,我终于弄明白了......

我的一个文件require('dotenv').config();在顶部。显然,这是用主.env文件中的任何内容覆盖 CRA 找到的配置。从我的代码中删除该行后,一切正常。

于 2021-03-18T09:44:37.587 回答