My React app is using values from the .env file instead of the .env.local file
Asked Answered
S

1

6

The dotenv module should be prioritizing my .env.local file over my .env file, but it's not. When I have REACT_APP_API_BASE set in both files, the app always uses the value in .env. It only uses the value in .env.local if I delete the matching definition in .env.


.env

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

.env.local

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

The app is making API calls to https://api-staging.mysite.com/api/endpoint.

What am I doing wrong?

For reference, I'm not running react-scripts directly, I'm using CRACO.

package.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
        }
      ]
    ]
  },
Subalternate answered 4/3, 2021 at 1:20 Comment(2)
why you want to use .env.local instead of .env ? .env.local or .env.example includes the examples and variables used in the projectNader
Because I want to temporarily overwrite the project defaults (or add in security tokens that shouldn't be saved to git).Subalternate
S
12

Five minutes after posting a bounty, I finally figure it out...

One of my files had require('dotenv').config(); at the top. Apparently, this was overwriting the configuration found by CRA with whatever was in the main .env file. Once I deleted that line from my code, everything worked fine.

Subalternate answered 18/3, 2021 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.