Netlift 构建:gatsby-source-prismic - 无效的插件选项 | Netlify 中的环境变量

Posted

技术标签:

【中文标题】Netlift 构建:gatsby-source-prismic - 无效的插件选项 | Netlify 中的环境变量【英文标题】:Netlift build: gatsby-source-prismic - Invalid plugin options | Environment variables in Netlify 【发布时间】:2021-04-09 04:01:44 【问题描述】:

当我尝试在 Netlify 上部署我的 Gatsby 站点时,它停止部署并抛出错误 gatsby-source-prismic - invalid plugin options

本地构建完美,我没有任何问题,我添加了一个带有 PRISMIC_REPOSITORY_NAMEPRISMIC_ACCESS_TOKEN.env 文件,其值来自 Prismic.io。

我的插件选项是:

      resolve: `gatsby-source-prismic`,
      options: 
        repositoryName: process.env.PRISMIC_REPOSITORY_NAME,
        accessToken: process.env.PRISMIC_ACCESS_TOKEN,
        schemas: 
          post: require("./src/schemas/articles.json"),
        ,
      ,
    ,

堆栈:

10:32:38 PM: Failed to validate error Error [ValidationError]: child "type" fails because ["type" must be one of [GRAPHQL, CONFIG, WEBPACK, PLUGIN]]
10:32:38 PM:     at Object.exports.process (/opt/build/repo/node_modules/@hapi/joi/lib/errors.js:202:19)
10:32:38 PM:     at internals.Object._validateWithOptions (/opt/build/repo/node_modules/@hapi/joi/lib/types/any/index.js:763:31)
10:32:38 PM:     at internals.Object.validate (/opt/build/repo/node_modules/@hapi/joi/lib/types/any/index.js:797:21)
10:32:38 PM:     at constructError (/opt/build/repo/node_modules/gatsby-cli/lib/structured-errors/construct-error.js:52:32)
10:32:38 PM:     at Reporter.error (/opt/build/repo/node_modules/gatsby-cli/lib/reporter/reporter.js:135:59)
10:32:38 PM:     at Reporter.panic (/opt/build/repo/node_modules/gatsby-cli/lib/reporter/reporter.js:75:34)
10:32:38 PM:     at Object.webhookBody [as panic] (/opt/build/repo/node_modules/gatsby/src/utils/api-runner-node.js:201:16)
10:32:38 PM:     at Object.exports.sourceNodes (/opt/build/repo/node_modules/gatsby-source-prismic/src/gatsby-node.ts:160:8)
10:32:38 PM:     at runAPI (/opt/build/repo/node_modules/gatsby/src/utils/api-runner-node.js:460:22)
10:32:38 PM:     at Promise.catch.decorateEvent.pluginName (/opt/build/repo/node_modules/gatsby/src/utils/api-runner-node.js:611:13)
10:32:38 PM:     at Promise._execute (/opt/build/repo/node_modules/bluebird/js/release/debuggability.js:384:9)
10:32:38 PM:     at Promise._resolveFromExecutor (/opt/build/repo/node_modules/bluebird/js/release/promise.js:518:18)
10:32:38 PM:     at new Promise (/opt/build/repo/node_modules/bluebird/js/release/promise.js:103:10)
10:32:38 PM:     at /opt/build/repo/node_modules/gatsby/src/utils/api-runner-node.js:609:16
10:32:38 PM:     at tryCatcher (/opt/build/repo/node_modules/bluebird/js/release/util.js:16:23)
10:32:38 PM:     at Object.gotValue (/opt/build/repo/node_modules/bluebird/js/release/reduce.js:166:18) 
10:32:38 PM:   isJoi: true,
10:32:38 PM:   details: [
10:32:38 PM:     
10:32:38 PM:       message: '"type" must be one of [GRAPHQL, CONFIG, WEBPACK, PLUGIN]',
10:32:38 PM:       path: [Array],
10:32:38 PM:       type: 'any.allowOnly',
10:32:38 PM:       context: [Object]
10:32:38 PM:     
10:32:38 PM:   ],
10:32:38 PM:   _object: 
10:32:38 PM:     context: ,
10:32:38 PM:     value: undefined,
10:32:38 PM:     type: 'string',
10:32:38 PM:     path: [],
10:32:38 PM:     branch: [ undefined ],
10:32:38 PM:     failures: [Function: failures],
10:32:38 PM:     pluginName: 'gatsby-source-prismic',
10:32:38 PM:     text: 'There was an error',
10:32:38 PM:     level: 'ERROR',
10:32:38 PM:     stack: [],
10:32:38 PM:     docsUrl: 'https://gatsby.dev/issue-how-to'
10:32:38 PM:   ,
10:32:38 PM:   annotate: [Function]
10:32:38 PM: 
10:32:38 PM: not finished source and transform nodes - 0.127s
10:32:38 PM: error Command failed with exit code 1.

我希望有人可以帮助我解决这个问题。我不知道是什么错误。

【问题讨论】:

你能补充更多细节吗?您是否在 Netlify 中正确设置了您的 .env?它是在本地构建的吗?等 对不起,这是我的错,是的,它在本地构建得很好,我添加了一个带有“PRISMIC_ACCESS_TOKEN”和“PRISMIC_REPOSITORY_NAME”的.env文件 【参考方案1】:

使用 Gatsby + Netlify 处理环境变量时,您必须在它们前面加上 GATSBY_ 以使它们可用于 Netlify。所以:

PRISMIC_ACCESS_TOKEN
PRISMIC_REPOSITORY_NAME

会变成:

GATSBY_PRISMIC_ACCESS_TOKEN
GATSBY_PRISMIC_REPOSITORY_NAME

在您的 .env 文件中本地更改它们并在 Netlify 旁边:

注意:您可以在https://app.netlify.com/sites/[YOUR_PROJECT]/settings/deploys#environment中访问该配置

资源:

https://www.gatsbyjs.com/docs/how-to/local-development/environment-variables/ https://community.netlify.com/t/how-to-access-environment-variables-in-a-react-component/1635

【讨论】:

非常感谢,这解决了我的部署问题

以上是关于Netlift 构建:gatsby-source-prismic - 无效的插件选项 | Netlify 中的环境变量的主要内容,如果未能解决你的问题,请参考以下文章

47-Jenkins-终止构建并设置构建结果

47-Jenkins-终止构建并设置构建结果

Groovy构建工具 ( 构建工具引入 | Gradle 构建工具作用 | 传统的依赖管理 )

每次构建项目时如何避免构建和构建通用框架?

使用自动工具构建系统/在单独的“构建”目录中构建

jenkins 构建触发器 以及参数的传递