user14593966
user14593966

Reputation: 151

Gatsby build command - contentful plugin not getting spaceId & accessToken from .env file

I've been working on this for several hours and have been trying out a number of suggestions in similar questions here.

I can run my gatsby site develop build just fine, but when I run "gatsby build" I'm running into this error.

Invalid plugin options for "gatsby-source-contentful":

- "accessToken" is required
- "spaceId" is required

not finished open and validate gatsby-configs, load plugins - 1.427s

I have one .env file named ".env" with both of the necessary env variables, and in my gatsby-config file, I have it configured like this...

resolve: 'gatsby-source-contentful',
      options: {
        spaceId: process.env.CONTENTFUL_SPACE_ID,
        accessToken: process.env.CONTENTFUL_ACCESS_TOKEN
      }

I've tested out the accessToken and spaceId by manually adding them into my gatsby-config plugins options and ran "gatsby build" and they are working fine. When I change the config back to the env variables though, it's no good and I get the error I mentioned previously.

One thing I tried just recently was to make another .env file and name it ".env.production" and it threw a different error like this...

“error Cannot access Contentful space “*********ab” on environment “master” with access token “***************************************1234”. Make sure to double check them! (value)”

This led me to look into the master environment on contentful but I'm pretty new to it and it's got me confused.

I can find a lot of questions here with the same type of problem but I'm yet to find a solution that works. Any gatsby experts out there who've had this problem?

Upvotes: 3

Views: 724

Answers (1)

Ferran Buireu
Ferran Buireu

Reputation: 29320

According to Environment variables docs:

In development, Gatsby will load environment variables from a file named .env.development. For builds, it will load from .env.production.

So, in your scenario, just duplicate and rename your .env file to .env.development and .env.production. Gatsby will take the needed data depending on the fired command.

Upvotes: 2

Related Questions