Look, I have my reasons, but mainly it’s because I enjoy walking up steep hills and hunting for needles in fields of haystacks! That aside, whilst trying to get one theme going, I ran into a problem.
It started with this pretty example: gatsby-v2-starter-theme but after you follow the instructions to install it, you run straight into an error:
"gatsby-node.js" threw an error while running the onCreateNode lifecycle: Cannot read property 'slug' of undefined slug gatsby-starter-v2-lumen
Eek! If you take a look at that file (located in the root of the theme at line 106), you see what it is complaining about.
let slug = fileNode.fields.slug
At least as far as I could tell, that field just doesn’t exist so I did a little hunting around:
- Examining the history of that file on GitHub
- Seeing what the Gatsby docs had to say about that object (no mention of slug)
…and drew a blank. Admittedly, my experience with Gatsby is borderline zero right now, but here’s what I came up with to try and resolve it.
I took this code:
const fileNode = getNode(node.parent)
and changed it to this:
let slug = 'unknown'
My intentions were that we would at least have a value for
slug even if it couldn’t find anything better.
Next, if we could find a
slug referenced in the MDX file, we would use that. By that I mean this part of your Markdown file:
Should I arbitrarily be creating my own fields in the front matter (the bit above)? Erm. Not sure, haha, but why not?
And failing that, we’d take the
Whatever you do, make sure you have a path, though - super required.
Anyway, re-run your build, and all should be well.
Hi! Did you find this useful or interesting? I have an email list coming soon, but in the meantime, if you ready anything you fancy chatting about, I would love to hear from you. You can contact me here or at stephen ‘at’ logicalmoon.com