Gatsby v2 - error building static HTML for pages failed using markdown

4.2k Views Asked by At

GraphiQL filesystem

This is my first attempt at using Gatsby v2. I have a test.md file which was created by the netlify cms, that's in the blog folder as you see in the filesystem above.

Also I can query the data using GraphiQL in the browser as above.

I get a list of blog posts(1) which is a link but there's no page generated to go to. When I run gatsby build I get an error.

error Building static HTML for pages failed

   6 | }) {
   7 |   const { markdownRemark } = data // data.markdownRemark holds our post data
>  8 |   const { frontmatter, html } = markdownRemark
     |           ^
   9 |   return (
  10 |     <div className="blog-post-container">
  11 |       <div className="blog-post">

WebpackError: TypeError: Cannot read property 'frontmatter' of null

I've looked all over github issues and tried to debug in VSCode but no luck...

Here are the relevant files.

gatsby-config.js

module.exports = {
    siteMetadata: {
        title: `simco-cms`,
    },
    plugins: [
        `gatsby-plugin-netlify-cms`,
        `gatsby-transformer-remark`,
        {
            resolve: `gatsby-source-filesystem`,
            options: {
                path: `${__dirname}/blog`,
                name: "markdown-pages",
            },
        },
    ],
}

pages/index.js

import React from "react"

import { graphql } from "gatsby"
import PostLink from "../components/postLink"

const IndexPage = ({
  data: {
    allMarkdownRemark: { edges },
  },
}) => {
  const Posts = edges
    .filter(edge => !!edge.node.frontmatter.date)
    .map(edge => <PostLink key={edge.node.id} post={edge.node} />)

  return <div>{Posts}</div>
}

export default IndexPage

export const pageQuery = graphql`
  query {
    allMarkdownRemark(sort: { order: DESC, fields: [frontmatter___date] }) {
      edges {
        node {
          id
          excerpt(pruneLength: 250)
          frontmatter {
            date(formatString: "MMMM DD, YYYY")
            path
            title
          }
        }
      }
    }
  }
`

gatsby-node.js

const path = require("path")

exports.createPages = ({ actions, graphql }) => {
  const { createPage } = actions

  const blogPostTemplate = path.resolve(`src/templates/blogTemplate.js`)

  return graphql(`
    {
      allMarkdownRemark(
        sort: { order: DESC, fields: [frontmatter___date] }
        limit: 1000
      ) {
        edges {
          node {
            frontmatter {
              path
            }
          }
        }
      }
    }
  `).then(result => {
    if (result.errors) {
      return Promise.reject(result.errors)
    }

    result.data.allMarkdownRemark.edges.forEach(({ node }) => {
      createPage({
        path: node.frontmatter.path,
        component: blogPostTemplate,
        context: {}, // additional data can be passed via context
      })
    })
  })
}

templates/blogTemplate.js

import React from "react"
import { graphql } from "gatsby"

export default function Template({
  data, // this prop will be injected by the GraphQL query below.
}) {
  const { markdownRemark } = data // data.markdownRemark holds our post data
  const { frontmatter, html } = markdownRemark
  return (
    <div className="blog-post-container">
      <div className="blog-post">
        <h1>{frontmatter.title}</h1>
        <h2>{frontmatter.date}</h2>
        <div
          className="blog-post-content"
          dangerouslySetInnerHTML={{ __html: html }}
        />
      </div>
    </div>
  )
}

export const pageQuery = graphql`
  query($path: String!) {
    markdownRemark(frontmatter: { path: { eq: $path } }) {
      html
      frontmatter {
        date(formatString: "MMMM DD, YYYY")
        path
        title
      }
    }
  }
`
1

There are 1 best solutions below

1
On BEST ANSWER

The path being resolved to your markdown is relative to the root of your project/repository location. The path passed to the template must be an absolute path for the query to be able to resolve the data at $path.

blog/test.md

---
path: test
date: 2018-11-05T16:25:21.303Z
title: test
---
test  

If you really want your path to be /test for this blog post then change the path:

---
path: /test
date: 2018-11-05T16:25:21.303Z
title: test
---
test  

Otherwise change it to path: /blog/test