Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
        }
      ]
    ]
  },
like image 424
carpiediem Avatar asked Mar 04 '21 01:03

carpiediem


People also ask

How do I view a .env variable in React?

You can access environment variables (with the REACT_APP_ prefix) from your React app via the Node. js process. env. object.


Video Answer


1 Answers

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.

like image 175
carpiediem Avatar answered Nov 09 '22 21:11

carpiediem