“gatsby-source-contentful”的插件选项无效

Bla*_*cey 9 access-token reactjs contentful gatsby gatsby-plugin

我在尝试打开通过 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
Run Code Online (Sandbox Code Playgroud)

我已经进行了多次编辑,但无法处理该项目,因为我目前无法打开它。我确实有一个内容丰富的帐户,但对 Gatsby 相当陌生,并且不知道如何为accessToken.

我需要通过 来执行此操作process.env,还是我完全错过了该过程?

谢谢,如有任何帮助,我们将不胜感激。

Fer*_*reu 10

我需要通过 来执行此操作process.env,还是我完全错过了该过程?

当然,您需要向 Gatsby 和 Contentful 提供您的访问令牌。默认情况下,Gatsby在运行时分别采用.env.development和,因此您需要将凭据添加到环境文件中。.env.productiongatsby developgatsby build

首先,在gatsby-node.js模块导出上方添加以下代码片段:

require("dotenv").config({
  path: `.env.${process.env.NODE_ENV}`,
})
Run Code Online (Sandbox Code Playgroud)

这将告诉 Gatsby 在每个运行的命令中需要获取哪个文件。

下一步是填充环境文件,在它们中添加:

CONTENTFUL_ACCESS_TOKEN=123456
CONTENTFUL_SPACE_ID=78910
Run Code Online (Sandbox Code Playgroud)

所以,最后你的gatsby-config.js应该是这样的:

// 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,
      },
    },
  ],
}
Run Code Online (Sandbox Code Playgroud)