I set up .env file and gatsby-config.js as below.
// .env.development
GATSBY_API_URL=https://example.com/api
A few steps & notes that should solve your problem:
console.log(process.env)
will always print empty objectTo see if it's really working, you should print the variables directly, e.g. console.log(process.env.API_URL)
.
In other words, your folder hierarchy should look something like:
.env.development
.env.production
src/
pages/
index.js
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.
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).
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
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
you may add your env key to the allow list in gatsby-config.js
{
resolve: `gatsby-plugin-env-variables`,
options: {
allowList: [
"XXXXXXXXX",
]
}
}
In Gatsby, availability of your environment variables depends on many factors:
GATSBY_
are treated differently..env.production
and .env.development
.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.
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.
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.
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.