loading...
Cover image for Migration to Gatsby v2 - Variable "$slug" of required type "String!" was not provided

Migration to Gatsby v2 - Variable "$slug" of required type "String!" was not provided

rajeshroyal profile image Rajesh Royal ・1 min read

when you run gatsby develop, you received the all data without these errors, but when use gatsby build you received this error.

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

solution

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

you may be creating pages with gatsby page API.

result.data.allWordpressPost.edges.forEach(({ node }) => {
      createPage({
        // Decide URL structure
        path: node.slug,
        // path to template file do not put templates in pages
           folder.
        component: path.resolve("./src/templates/blog.js"),
        context: {
          slug: node.slug,
          $slug: node.slug
        },
      });
    });

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!

Discussion

pic
Editor guide