エラーの内容
Cannot read property 'dir' of null 18 | thumbnail { | ^ 19 | childImageSharp { 20 | gatsbyImageData(width: 1000, formats: [AUTO, WEBP, AVIF]) 21 | } 22 | }
状況
npm run developだとエラーは起きず、画像が表示され意図した挙動になっています
npm run buildすると、エラーになります。
ディレクトリ構成
content/blog/first/index.md
content/blog/first/top.jpg
index.md
1--- 2thumbnail: "./top.jpg" 3---
gatsby-node.js
type Frontmatter { thumbnail: File @fileByRelativePath }
graphql
js
1export const pageQuery = graphql` 2 query BlogPostBySlug( 3 $id: String! 4 $previousPostId: String 5 $nextPostId: String 6 ) { 7 site { 8 siteMetadata { 9 title 10 } 11 } 12 markdownRemark(id: { eq: $id }) { 13 id 14 html 15 frontmatter { 16 thumbnail { 17 childImageSharp { 18 gatsbyImageData(width: 1000, formats: [AUTO, WEBP, AVIF]) 19 } 20 } 21 } 22 } 23 } 24`
const thumbnail = getImage(data.markdownRemark.frontmatter.thumbnail) ~~~~~~~ <GatsbyImage className="m-auto" image={thumbnail} alt={post.frontmatter.title} ></GatsbyImage>
補足
スターターはgatsby-starter-blogを使っています。
記事と記事に使う画像を同じディレクトリに保存したら楽かなと思ってこの構成にしていますが、難易度下がるなら画像の場所はどこでも良いかなと思っています。
もし思い当たる節がある方おりましたら、教えていただければと思います。どうぞよろしくお願いします。
あなたの回答
tips
プレビュー