.env
variables are strings so, you can't loop them and display what's inside each position like an array. You can set something like this in your .env.development
:
MESSAGES="message1, messages2, messages"
However, this would be one single string. You'll need to split them into a 3 position array by let dotEnvArray = process.env.MESSAGES.split(',')
.
If you use try: MESSAGES=["message1", "messages2", "messages3"]
it will be treated as MESSAGES= "['message1', 'message2', 'message3']"
.
What you are seeing here:
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-env-variables`,
options: {
whitelist: ["MY_VAR", "MY_OTHER_VAR"]
},
},
],
}
Is an array of .env variables isolated. According to this plugin's documentation:
This will make MY_VAR
& MY_OTHER_VAR
available at runtime in your app
by accessing process.env.MY_VAR
or process.env.MY_OTHER_VAR
.
However, those variables haven't any value defined, you will need to set them in your .env.development
file anyway. This is because Gatsby by default only exposes all variables prefixed with GATSBY_
. Take a look to Gatsby documentation about .env
files for further information.
Answering another part of your question, once you've set (in your gatsby-config.js
:
require("dotenv").config({
path: `.env.${process.env.NODE_ENV}`,
})
By default, running gatsby develop
Gatsby will take your .env.development
variables and will expose them under process.env.VAR_NAME
, always isolated and treated as a single string, just how your CodeSandbox shows.
To achieve what you want in your repository, just add your environment variables in your whitelist.
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-env-variables`,
options: {
whitelist: ["ENDPOINTS"]
},
},
],
}