使用gatsby-casper starter搭建项目报错AuthorYaml类型不存在如何解决
错误本质说明
该报错是Gatsby构建GraphQL Schema阶段,没有找到AuthorYaml类型导致的,核心原因是yaml解析插件、文件源配置异常,或是缺少对应的作者yaml文件,导致Gatsby没有自动生成该类型。
排查步骤
- 先重装完整依赖,删除本地依赖缓存文件后重新安装,避免starter初始化时依赖拉取不全:
rm -rf node_modules package-lock.json yarn.lock npm install
- 检查
gatsby-config.js中的数据源配置,确认存在指向作者文件目录的gatsby-source-filesystem配置,没有的话手动补上:
{ resolve: `gatsby-source-filesystem`, options: { path: `${__dirname}/content/authors`, name: `authors`, }, },
- 检查
content/authors目录是否存在,且目录下至少有1个后缀为.yml/.yaml的作者配置文件,文件字段要符合starter要求,至少包含name、avatar等基础字段。 - 确认
gatsby-transformer-yaml插件已经安装且在gatsby-config.js的plugins数组中注册,未安装的话执行npm install gatsby-transformer-yaml安装后补到配置里。 - 清除Gatsby本地缓存重启服务,缓存异常是Gatsby schema类问题的常见诱因:
gatsby clean gatsby develop
兜底解决方法
如果以上步骤都无法解决,可手动声明AuthorYaml类型,避免Gatsby因为没有对应数据自动跳过类型生成:在项目根目录的gatsby-node.js中加入以下代码即可:
exports.createSchemaCustomization = ({ actions }) => { const { createTypes } = actions const typeDefs = ` type AuthorYaml implements Node { name: String avatar: File @fileByRelativePath bio: String url: String twitter: String facebook: String instagram: String } ` createTypes(typeDefs) }
内容的提问来源于stack exchange,提问作者Night Coder
相关产品推荐
相关产品推荐

