reactjsgatsbyaccess-tokencontentfulgatsby-plugin

Invalid plugin options for "gatsby-source-contentful"


I'm encountering the following error when attempting to open a project I forked via GitHub.

success open and validate gatsby-configs - 0.492s

 ERROR #11331  PLUGIN

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

- "accessToken" is required

not finished load plugins - 6.220s

I've made several edits but am unable to work on the project as I'm unable to open it at the moment. I do have a contentful account, but am fairly new to Gatsby and am unaware of how to set a new value for the accessToken.

Would I need to do this via process.env, or am I missing the process entirely?

Thank you, any help is appreciated.


Solution

  • Would I need to do this via process.env, or am I missing the process entirely?

    Absolutely, you need to provide to Gatsby and Contentful your access tokens. Gatsby, by default, takes the .env.development and .env.production when running gatsby develop and gatsby build respectively, so you will need to add the credentials to the environment files.

    First of all, add the following snippet in your gatsby-node.js, above the module exportation:

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

    This will tell Gatsby which file needs to be taken in each running command.

    The next step is to fill the environment files, in both of them add:

    CONTENTFUL_ACCESS_TOKEN=123456
    CONTENTFUL_SPACE_ID=78910
    

    So, finally your gatsby-config.js should look like:

    // In your gatsby-config.js
    module.exports = {
      plugins: [
        {
          resolve: `gatsby-source-contentful`,
          options: {
            spaceId: process.env.CONTENTFUL_SPACE_ID,
            accessToken: process.env.CONTENTFUL_ACCESS_TOKEN,
          },
        },
      ],
    }