gatsby: accessToken: undefined - "accessToken" is required

Description

accessToken: undefined - “accessToken” is required

Steps to reproduce

Since today all the plugins seems to make me problem, i just don’t understand why is this happening first project of mine in Gatsby and i have experienced all the problems till now… this time the error is accessToken: undefined - “accessToken” is required i uninstalled gatsby-source-contentful and install it again i have still the same problem… Before i had with env-cmd i fixed that, then with sharp module, i fixed that now this… please anyone help 😦

module.exports = { siteMetadata: { title: "Shanti-Hostel", author: "Dina Shantina", }, plugins: [ "gatsby-plugin-react-helmet", { resolve: "gatsby-source-contentful", options: { spaceId: process.env.CONTENTFUL_SPACE_ID, accessToken: process.env.CONTENTFUL_ACCESS_TOKEN, }, }, "gatsby-plugin-sass", { resolve: "gatsby-source-filesystem", options: { name: "src", path: ${__dirname}/src/, }, }, "gatsby-plugin-sharp", { resolve: "gatsby-transformer-remark", options: { plugins: [ "gatsby-remark-relative-images", { resolve: "gatsby-remark-images", options: { maxWidth: 750, linkImagesToOriginal: false, }, }, ], }, }, ], }

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 27 (1 by maintainers)

Most upvoted comments

If you’re on Windows and you have this issue, I did too and I just fixed it by following this Gatsby doc on setting up environment variables: https://www.gatsbyjs.org/docs/environment-variables/.

Added this to my gatsby-config.js file:

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

Everything worked fine afterwards. @kkartikeye maybe this would help!

I’m a Windows 10 user, I followed @Paahn gist exactly, and it worked perfectly to solve this problem for me. Seems to me running the command ‘env-cmd gatsby develop/build’ causes a conflict. Setting the config properly as Paahn did will allow .env variables to load during build time. Most of the older tutorials like Andrew Mead’s Great Gatsby showed differently, so this may be the cause of some of the confusion. A lot has changed in the last year to the build process.

I was having the same trouble with DinaShatina and I solved it by moving the let activeEnv declaration to the top of the gatsby-config.js file. Find full code here

Thanks all for helping DinaShatina out!

I encounter this issue as well but I noticed it’s coming from the way I run the command, I have this in my package.json develop: "env-cmd --file .env.development gatsby develop".

If I run gatsby develop, I got errors because it can’t read the .env file variables hence, it throws undefined, but when I use yarn develop. Everything seems to work fine.

Ok so started the folder i just reinstalled the npm again i put again the variables and its working now… So tnx everyone for the help…

@DinaShantina sorry for the issues you’re experiencing. I’ve added the same dependencies that you supplied and copied over the content of gatsby-config.js, you have provided it looks like you have some configuration errors.

Your gatsby-config.js should look like:

const dotenv = require("dotenv").config()
module.exports = {
  siteMetadata: {
    title: "Shanti-Hostel",
    author: "Dina Shantina",
  },
  plugins: [
    "gatsby-plugin-react-helmet",
    {
      resolve: "gatsby-source-contentful",
      options: {
        spaceId: process.env.CONTENTFUL_SPACE_ID,
        accessToken: process.env.CONTENTFUL_ACCESS_TOKEN,
      },
    },
    "gatsby-plugin-sass",
    {
      resolve: "gatsby-source-filesystem",
      options: {
        name: "src",
        path: `${__dirname}/src/`, // <= error was here
      },
    },
    "gatsby-plugin-sharp",
    {
      resolve: "gatsby-transformer-remark",
      options: {
        plugins: [
          "gatsby-remark-relative-images",
          {
            resolve: "gatsby-remark-images",
            options: {
              maxWidth: 750,
              linkImagesToOriginal: false,
            },
          },
        ],
      },
    },
  ],
}