Can't access gatsby environment variables on the client side

前端 未结 5 1167
旧时难觅i
旧时难觅i 2021-02-19 00:44

I set up .env file and gatsby-config.js as below.

// .env.development
GATSBY_API_URL=https://example.com/api


        
相关标签:
5条回答
  • 2021-02-19 01:14

    A few steps & notes that should solve your problem:

    console.log(process.env) will always print empty object

    To see if it's really working, you should print the variables directly, e.g. console.log(process.env.API_URL).

    Make sure .env.* is in your root folder

    In other words, your folder hierarchy should look something like:

    .env.development
    .env.production
    src/
      pages/
        index.js
    

    You don't need to prefix with GATSBY_ if you want to access env variables server-side

    From the docs:

    In addition to these Project Environment Variables defined in .env.* files, you could also define OS Env Vars. OS Env Vars which are prefixed with GATSBY_ will become available in browser JavaScript.

    You need the GATSBY_* prefix if you are using them browser-side

    The prefixing is only if you use the OS Env Vars approach (i.e. you set them directly on your server and not in these .env files).

    Kill and restart gatsby develop when you've added the .env file(s)

    I ran into this when reproducing on CodeSandbox (in CodeSandbox, you do the restart by going to Server Control Panel on the left, and clicking Restart Sandbox).

    Here's the working example: https://codesandbox.io/s/jj8xzn2y15

    0 讨论(0)
  • 2021-02-19 01:17

    Maybe worth noting that it's easy to misname the file if you are used to writing .dev or .develop.

    Gatsby requires that the file is named exactly: .env.development

    0 讨论(0)
  • 2021-02-19 01:19

    you may add your env key to the allow list in gatsby-config.js

    { 
      resolve: `gatsby-plugin-env-variables`,
        options: {
          allowList: [
            "XXXXXXXXX", 
          ]
        }
    }
    
    0 讨论(0)
  • 2021-02-19 01:30

    In Gatsby, availability of your environment variables depends on many factors:

    • Execution context. Are you trying to reach the variable client-side or server-side?
    • Variable name. Names starting with GATSBY_ are treated differently.
    • Environment file name. By default, Gatsby expects these to be .env.production and .env.development.
    • DotEnv usage. Whether you're using dotenv npm package to load the env file.

    Here are the different ways to make an environment variable accessible in server and browser environments. Choose whichever fits your setup best.

    Server-side:

    1. Define an OS environment variable.

    MY_VAR='my value'; export MY_VAR // will persist in the OS environment
    npm run start
    

    OR

    MY_VAR='my value' npm run start // will set the variable for that process only
    

    Note: variable name doesn't matter.

    2. Create an env file and define variable there.

    echo MY_VAR='my value' >> .env.development
    

    Load the file in gatsby-config.js:

    require('dotenv').config({
      path: `.env.${process.env.NODE_ENV}`,
    })
    

    Note: file name doesn't matter. Variable name doesn't matter.

    Client-side:

    1. Create a .env.development (.env.production for prod environment) file and define a variable there.

    Note: file name does matter. Variable name doesn't matter.

    2. Create an environment file named differently (say .env) and define a GATSBY_ prefixed variable there.

    echo GATSBY_MY_VAR='my value' >> .env
    

    Load the file in gatsby-config.js with dotenv.

    require('dotenv').config() // .env is loaded by default, no need to specify path
    

    Note: file name does not matter. Variable name does matter.

    3. Define a GATSBY_ prefixed OS environment variable.

    GATSBY_MY_VAR='my value'; export GATSBY_MY_VAR
    npm run start
    

    OR

    GATSBY_MY_VAR='my value' npm run start
    

    Note: variable name does matter.

    0 讨论(0)
  • 2021-02-19 01:37

    Make sure you've included

    require("dotenv").config({ path: .env.${process.env.NODE_ENV}, })

    in your gatsby-config.js file before you start using your ENV variables.

    0 讨论(0)
提交回复
热议问题