0

Why does process.env not see my environments from the .env file? I already added REACT_APP_API_KYE = 'some-code-example' in the file, but when I try to use it in the api request, all apps are down. I did restart my app after creating the .env file.

.env

REACT_APP_API_KEY = 'euk4h43l-ertek8-rgde45-gkr3945';

.common.api.ts

export const instance = axios.create({
    baseURL: 'xxxxxxx',
    withCredentials: true,
    headers: {
        'API-KEY': process.env.REACT_APP_API_KEY
    }
})
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132

2 Answers2

0

Have you added the dotenv package to your package.json, and called dotenv.config() at the beginning of your script?

// npm install dotenv
import * as dotenv from 'dotenv' // import

dotenv.config() // load .env into process.env

export const instance = axios.create({
    baseURL: 'xxxxxxx',
    withCredentials: true,
    headers: {
        'API-KEY': process.env.REACT_APP_API_KEY // access
    }
})
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0

you need to install

npm install dotenv --save

after this add to your app.

require('dotenv').config()
todary
  • 1
  • 3