2

using variable inside .env file. In create react app.

I am trying to re use the env variable in other env variables, is this correct of way of doing, but i am not getting the value .

.env

A=hello
B=${A}/new
11
  • Could you try with require('dotenv').config() if you didn't ? Commented Jan 30, 2020 at 11:04
  • so i need to install dotenv package additionally and use it isn't Commented Jan 30, 2020 at 11:06
  • this is indeed a fun question... I honestly don't know if it is even possible, I've never seen this before. However, it's only a env file. Whenever you need to change some env variable, you'll need to change this file, so why don't just put B=hello/new ? Commented Jan 30, 2020 at 11:21
  • 1
    @AhmetZeybek create-react-app is already using dotenv Commented Jan 30, 2020 at 11:26
  • @DiogoLessa trying the impossible things is everyday's developer job thats how the requirement comes hahha :) and thats how people think of javascript Commented Jan 30, 2020 at 11:30

1 Answer 1

2

It's important to realize that env variables are only available in node environment and not in the browser.

But if you're using create-react-app, It does some tricks for you and simulates the same behavior inside the browser, only if you put the REACT_APP_ prefix before your env variables.

As the create-react-app docs says here:

Note: You must create custom environment variables beginning with REACT_APP_. Any other variables except NODE_ENV will be ignored to avoid accidentally exposing a private key on the machine that could have the same name. Changing any environment variables will require you to restart the development server if it is running.

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

3 Comments

that i know but if i add the prefix with REACT_APP will it work ?
@DILEEPTHOMAS check this doc
Yes. @DILEEPTHOMAS, when create-react-app includes your env files in your final bundle, it ignores the ones that don't have REACT_APP_ prefix.

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.