0

I am trying to solve this issue for days. I never wrote a gatsby-node.js but used a Gatsby NetlifyCMS starter that came with it. From what I understand it's probably supposed to create a page from my markdown data automatically.

When I add code to my static/admin/config.yml (the last bit), add content in the admin and pull, i get this error:

I would truly appreciate any help on this.

Your site's "gatsby-node.js" created a page with a component that
doesn't exist.

The path to the missing component is
"/home/hanna/Code/brnhrz-cms/src/templates/null.js"
The page object passed to createPage:
{
    "path": "/events/events/",
    "tags": null,
    "component": "/home/hanna/Code/brnhrz-cms/src/templates/null.js",
    "context": {
        "id": "331ab6a0-648d-5ed0-9613-09abbdee8c18"
    }
}

My config.yml:

backend:
  name: git-gateway
  branch: master
  commit_messages:
    create: "Create {{collection}} “{{slug}}”"
    update: "Update {{collection}} “{{slug}}”"
    delete: "Delete {{collection}} “{{slug}}”"
    uploadMedia: "[skip ci] Upload “{{path}}”"
    deleteMedia: "[skip ci] Delete “{{path}}”"

local_backend: true
media_folder: static/img
public_folder: /img

collections:
  - name: "blog"
    label: "Blog"
    folder: "src/pages/blog"
    create: true
    slug: "{{year}}-{{month}}-{{day}}-{{slug}}"
    fields:
      - {
          label: "Template Key",
          name: "templateKey",
          widget: "hidden",
          default: "blog-post",
        }
      - { label: "Title", name: "title", widget: "string" }
      - { label: "Publish Date", name: "date", widget: "datetime" }
      - { label: "Description", name: "description", widget: "text" }
      - { label: "Featured Post", name: "featuredpost", widget: "boolean" }
      - { label: "Featured Image", name: "featuredimage", widget: image }
      - { label: "Body", name: "body", widget: "markdown" }
      - { label: "Tags", name: "tags", widget: "list" }
  - name: "pages"
    label: "Pages"
    files:
      - file: "src/pages/events/events.md"
        label: "Events"
        name: "events"
        fields:
          - {
              label: "Template Key",
              name: "templateKey",
              widget: "hidden",
              default: "events-page",
                }
              - { label: Title, name: title, widget: markdown }
              - { label: Description, name: description, widget: markdown }
(...code for other pages that came already with the starter)

my gatsby-node.js:

const _ = require('lodash')
const path = require('path')
const { createFilePath } = require('gatsby-source-filesystem')
const { fmImagesToRelative } = require('gatsby-remark-relative-images')

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

  return graphql(`
    {
      allMarkdownRemark(limit: 1000) {
        edges {
          node {
            id
            fields {
              slug
            }
            frontmatter {
              tags
              templateKey
            }
          }
        }
      }
    }
  `).then((result) => {
    if (result.errors) {
      result.errors.forEach((e) => console.error(e.toString()))
      return Promise.reject(result.errors)
    }

    const posts = result.data.allMarkdownRemark.edges

    posts.forEach((edge) => {
      const id = edge.node.id
      createPage({
        path: edge.node.fields.slug,
        tags: edge.node.frontmatter.tags,
        component: path.resolve(
          `src/templates/${String(edge.node.frontmatter.templateKey)}.js`
        ),
        // additional data can be passed via context
        context: {
          id,
        },
      })
    })

    // Tag pages:
    let tags = []
    // Iterate through each post, putting all found tags into `tags`
    posts.forEach((edge) => {
      if (_.get(edge, `node.frontmatter.tags`)) {
        tags = tags.concat(edge.node.frontmatter.tags)
      }
    })
    // Eliminate duplicate tags
    tags = _.uniq(tags)

    // Make tag pages
    tags.forEach((tag) => {
      const tagPath = `/tags/${_.kebabCase(tag)}/`

      createPage({
        path: tagPath,
        component: path.resolve(`src/templates/tags.js`),
        context: {
          tag,
        },
      })
    })
  })
}

exports.onCreateNode = ({ node, actions, getNode }) => {
  const { createNodeField } = actions
  fmImagesToRelative(node) // convert image paths for gatsby images

  if (node.internal.type === `MarkdownRemark`) {
    const value = createFilePath({ node, getNode })
    createNodeField({
      name: `slug`,
      node,
      value,
    })
  }
}

1 Answers1

0

The fact that is complaining about:

"/home/hanna/Code/brnhrz-cms/src/templates/null.js"

Indicates that your component is null at:

component: path.resolve(
  `src/templates/${String(edge.node.frontmatter.templateKey)}.js`
),

So ${String(edge.node.frontmatter.templateKey)} is null.

There are multiple possible sources that cause that issue. First of all, check the query in your GrahiQL environment to check that is returning a valid value in all the events you are creating because if one is null, it will break your code.

Also double-check the created markdown to see if it's properly formatted or it contains all the mandatory fields (specially the templateKey) and add it if it doesn't.

Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67