DEV Community

Discussion on: Variable "$slug" of required type "String!" was not provided.

Collapse
 
rajeshroyal profile image
Rajesh Royal • Edited

I found the solution

The issue is you should not put template files in src/pages folder.

It so happens that I have already moved my single-post.js, post.js and other CPT .js files into a new, custom folder (/src/templates).

If this didn't work then remove ! mark from ($slug: String)

Thank you!

Thread Thread
 
lparolari profile image
Luca Parolari

Hello, I'm experiencing the same issue as the author of the post. I found your solution works, but I would like to know why this is happening and why moving the template file to another directory solves the problem.

Thank you!

Thread Thread
 
rajeshroyal profile image
Rajesh Royal

Don't know brother, It's been awhile since I'm using gatsby.

Thread Thread
 
greenjohn profile image
John Yellow

Hi Luca and Rajesh, you guys really saved me here. I had been 2 days already with this error, I even asked in the gatsby Discord, but you guys were eventually the saviors!

I think I know what's going on. Gatsby tries to render all the files in src/pages as pages. When it gets to the templates that are used by createPage, it freaks out because the context that you passed from is not there (createPage hasn't been called at this point). So when you move these files to another directory, Gatsby no longer tries to render your templates as pages and only invokes them after createPage has been called.

Does that make sense?