我已经为此工作了几个小时,并在这里尝试了一些类似问题的建议。
我可以很好地运行我的 gatsby 站点开发构建,但是当我运行“gatsby build”时,我遇到了这个错误。
Invalid plugin options for "gatsby-source-contentful":
- "accessToken" is required
- "spaceId" is required
not finished open and validate gatsby-configs, load plugins - 1.427s
我有一个名为“.env”的 .env 文件,其中包含两个必要的 env 变量,在我的 gatsby-config 文件中,我将它配置为这样...resolve: 'gatsby-source-contentful',
options: {
spaceId: process.env.CONTENTFUL_SPACE_ID,
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN
}
我已经通过将 accessToken 和 spaceId 手动添加到我的 gatsby-config 插件选项中并运行了“gatsby build”来测试它们,并且它们工作正常。但是,当我将配置更改回 env 变量时,这并不好,并且出现了我之前提到的错误。我最近尝试的一件事是制作另一个 .env 文件并将其命名为“.env.production”,它抛出了一个不同的错误,如下所示......
“error Cannot access Contentful space “*********ab” on environment “master” with access token “***************************************1234”. Make sure to double check them! (value)”
这让我在 contentful 上研究了 master 环境,但我对它很陌生,这让我感到困惑。我可以在这里找到很多具有相同类型问题的问题,但我还没有找到有效的解决方案。有没有遇到过这个问题的 gatsby 专家?
最佳答案
根据 Environment variables docs :
In development, Gatsby will load environment variables from a file named
.env.development
. For builds, it will load from.env.production
.
因此,在您的场景中,只需复制并重命名您的
.env
文件到 .env.development
和 .env.production
. Gatsby 将根据触发的命令获取所需的数据。
关于reactjs - Gatsby 构建命令 - 内容丰富的插件无法从 .env 文件中获取 spaceId 和 accessToken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68790069/