Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 514
Madrus Avatar asked Dec 20 '18 14:12

Madrus


People also ask

What version of node does Gatsby use?

If you don't set a Node version, Gatsby cloud will use the following defaults: The default Node. js version is v14 . If you are using NPM 7, the default is v16.

Does Gatsby use node JS?

Gatsby is built using JavaScript, and requires the Node. js runtime. Installing Node. js also installs npm, the Node.

What is Gatsby node js file for?

js / gatsby-node. ts is run once in the process of building your site. You can use its APIs to create pages dynamically, add data into GraphQL, or respond to events during the build lifecycle.


1 Answers

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)
  }
}
like image 67
Madrus Avatar answered Oct 14 '22 02:10

Madrus