Madrus
Madrus

Reputation: 746

getNode() method not found in gatsby-node.js with the latest version of Gatsby

Am working through the official Gatsby tutorial here. Up until step 7 everything worked 100% fine. In step 7 "Programmatically create pages from data", this snippet is listed for gatsby-node.js (as is, no imports):

exports.onCreateNode = ({ node }) => {
  if (node.internal.type === `MarkdownRemark`) {
    const fileNode = getNode(node.parent)
    console.log(`\n`, fileNode.relativePath)
  }
}

However, when running gatsby develop I get: ReferenceError: getNode is not defined. I have googled it up for quite some time, and it seems that there may have been some breaking changes recently in the latest versions of Gatsby. Does anyone have an idea what could be the reason for this and how to fix the missing reference? Maybe some module should be imported?

Upvotes: 6

Views: 2594

Answers (1)

Madrus
Madrus

Reputation: 746

Have just figured out the answer. It was my own typo. I did not add the second getNode parameter to the onCreateNode function:

exports.onCreateNode = ({ node, getNode }) => {
  if (node.internal.type === `MarkdownRemark`) {
    const fileNode = getNode(node.parent)
    console.log(`\n`, fileNode.relativePath)
  }
}

Upvotes: 19

Related Questions