Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment variables - undefined

I want to use environment variables. I made a custom react app environment.

Everything is ok, the app runs properly, no errors. But the variables from .env file gives undefined and the process.env gives an empty object.

I added dotenv and REACT_APP prefix to the variable.

And in webpack.config.js file i added node: { fs: 'empty' }, from here

Here are my configurations.

Folder structure:

enter image description here


2 Answers

PROBLEM SOLVED:

  1. Uninstall dotenv
  2. Remove these two from main app.js file:
const dotenv = require('dotenv')
dotenv.config();
  1. Remove the flag --env from npm start script.

  2. Remove node: { fs: 'empty' } from webpack.config.js file

  3. Install dotenv-webpack, and follow the instructions from there.

No need for REACT_APP prefix.

Fixed configuration files

You have to put REACT_APP in front of the variable name you want to have

eg:/

REACT_APP_YOUR_VAR="something"

You don't need to install Dotenv or something else, because React has its own.

like image 32
Luthfi Avatar answered Oct 26 '25 03:10

Luthfi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!