18

I have a react js project , but with typescript. I understand we can create .env file and have some configuration defined as such,

.env file

REACT_APP_SOME_CONFIGURATION = "some value" 

and use it in the code , without importing anything , like below

const configValue = process.env.REACT_APP_SOME_CONFIGURATION 

I tried this set up in my project , and it didn't work. is it because it is typescript? how to use .env file in such scenario.

8
  • 1
    process.env does not exist on front end, you can use it only in your node backend Commented Oct 20, 2020 at 20:59
  • 5
    This is not true We use it also in Frontend Commented Oct 20, 2020 at 21:06
  • @yoel are you sure about it? we had issue with similar case in our project because FE (react) could not consume process.env. what am i missing? Commented Oct 20, 2020 at 21:11
  • 1
    @YakirFitousi see here create-react-app.dev/docs/adding-custom-environment-variables Commented Oct 20, 2020 at 21:17
  • 2
    @YakirFitousi hey yakir see here react-israel.co.il/… OR here stackoverflow.com/a/59244254/9161478 Commented Oct 25, 2020 at 9:50

5 Answers 5

22

In TypeScript, you need to set the return value so if this string did so

const configValue : string = process.env.REACT_APP_SOME_CONFIGURATION 

OR

const configValue: string = (process.env.REACT_APP_SOME_CONFIGURATION as string);

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

2 Comments

you're a life savior
the variable is possible undefined.
10

You can add this to your react-app-env.d.ts file

declare namespace NodeJS {
    interface ProcessEnv {
       //types of envs
        NODE_ENV: 'development' | 'production' | 'test';
        PUBLIC_URL: string;

    }
}

Comments

3

Update on March 2021: With React 17 and typescript, you don't need to set the return the 'configValue' value, just use the env variable like you did before. My .env file like this

REACT_APP_STRING=some_string_dont_have_quotation_mark

Comments

2

You could change this to a string or Undefined for a failing scenario like so

const configValue : string | undefined = process.env.REACT_APP_SOME_CONFIGURATION

Comments

0

If you are using some framework with react like NextJs be careful cause env variables may change suffix, in the case of next. js the suffix for the public scope is "NEXT_PUBLIC_" and so taking into account actual TypeScript versions where it's not that necessary to specify your var type your code should be

const configValue = process.env.NEXT_PUBLIC_SOME_CONFIGURATION

In case you prefer to be more correct and force the var to be a string, I would recommend

const configValue: string = (process.env.NEXT_PUBLIC_SOME_CONFIGURATION as string)

Comments

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.