skip to Main Content

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
    }
})

2

Answers


  1. 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
        }
    })
    
    Login or Signup to reply.
  2. you need to install

    npm install dotenv --save
    

    after this add to your app.

    require('dotenv').config()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search