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