0

I made a basic Node.js/React project, and my .env file doesn't work. My project is at https://github.com/TheGElCOgecko/weather-predictor/tree/main. The main details is that I am using dotenv-webpack, my .env file is in the root directory, the project is a React/Node.js project, whenever I try and print a value from the .env file, it prints "undefined" (I print it in App.js), and when I deployed it on GitHub Pages using "npm run deploy", it straight up deleted my .env file.

I tried using dotenv instead of dotenv-webpack, but that didn't work and it wouldn't compile if I included "require('dotenv').config();". My webpack.config.js used to include this:

resolve: {
  fallback: {
    "path": require.resolve("path-browserify"),
    "os": require.resolve("os-browserify/browser"),
    "crypto": require.resolve("crypto-browserify"),
  }
},

I switched to dotenv-webpack because at least that was compiling (whereas using dotenv resulted in compilation failing), but maybe that was the wrong choice?

3
  • The question should contain all relevant code in addition to the repo, please, add it. It will become unintelligible for future readers when the link becomes unavailable Commented Aug 4, 2024 at 18:06
  • I believe I added everything that is relevant, I just wanted to link the repo in case I had a lack of understanding of what was relevant and what wasn't. With the answer resolved, it appears nothing more turned out to be relevant, and it was just me not knowing how React projects generally work. Commented Aug 4, 2024 at 20:17
  • At least webpack.config.json is relevant that the answer addresses Commented Aug 4, 2024 at 22:32

1 Answer 1

0

dotenv can't be used as you tried, It's not webpack plugin and doesn't support client-side apps:

plugins: [
    new Dotenv(),
]

This is what dotenv-webpack does, and you try to solve a problem that shouldn't exist in the first place.

The project is created with create-react-app, which is a preconfigured wrapper around Webpack, and uses its commands to run the app. CRA ignores webpack.config.json and doesn't need an additional configuration, unless proven otherwise. Internal Webpack configuration can be extended but this should be done in a special way, e.g. with Craco.

CRA already supports dotenv and implements the solution similar to dotenv-webpack that works out of the box. The problem is that environment variable doesn't take the requirement for REACT_APP_ prefix into account. Any unprefixed variables are ignored for security reasons.

REACT_APP_API_KEY_TEST should be defined in .env, then it will be available as process.env.REACT_APP_API_KEY_TEST in React app.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried that in the past and it did not work. However, I looked further into how to make this work as a React project (I've been focused on how to do it in a Node.js project since I thought that was the important thing), and I figured out I just had to restart my deployment to make it work. Thank you for the guidance!
You're welcome. Yes, in client-side apps, any changes outside src require to restart a devserver, unless this feature is additionally handled by a dev

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.