reactjs - Gatsby 构建命令 - 内容丰富的插件无法从 .env 文件中获取 spaceId 和 accessToken

标签 reactjs environment-variables gatsby contentful

我已经为此工作了几个小时,并在这里尝试了一些类似问题的建议。
我可以很好地运行我的 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/

相关文章:

javascript - 特定子项的特定数组 Prop [React]

java - Tomcat 和 CATALINA_HOME

powershell - Powershell环境变量继承

macos - 如何在OS X上取消设置环境变量?

nuxt.js - 当所有人都可以生成静态站点时,为什么我会选择 Gridsome/Gatsby 而不是 Nuxt/Next?

javascript - GatsbyJS 和 super 代理 : WebpackError: require is not a function

javascript - Gatsby Contentful - GraphQL 查询错误 : Unknown type "ContentfulFixed"

reactjs - react 教程 : CommentForm's onSubmit not working

javascript - react 上下文: Nested re-use/enhancement (anti-pattern?)

reactjs - 在 React Native 屏幕之间传递数据(从屏幕 A 到屏幕 B 到屏幕 C)