Skip to content

Gatsby Node API Error Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Gatsby Node API Error Fix. We cover key concepts, practical examples, and best practices.

The Problem

error Your site's "gatsby-node.js" is not exporting the "createPages" function.
Make sure it exports the "createPages" API via "exports.createPages = ..."

Gatsby does not find the expected Node API export in gatsby-<a href="/backend/nodejs/">node.js</a>.

Wrong

// gatsby-node.js
const createPages = ({ actions }) => {
  // create pages
}

Output: "gatsby-<a href="/backend/nodejs/">node.js</a>" is not exporting the "createPages" function.

The function is defined but not exported.

exports.createPages = async ({ actions, graphql }) => {
  const { createPage } = actions

  const result = await graphql(`
    query {
      allMarkdownRemark {
        nodes {
          frontmatter { slug }
        }
      }
    }
  `)

  result.data.allMarkdownRemark.nodes.forEach(node => {
    createPage({
      path: node.frontmatter.slug,
      component: require.resolve('./src/templates/post.js'),
      context: { slug: node.frontmatter.slug },
    })
  })
}

Output: pages are created dynamically based on GraphQL query results.

Prevention

  • Always use exports.createPages (or module.exports.createPages)
  • Export all Node API functions explicitly
  • Use exports.createPages for dynamic page creation and exports.onCreatePage for page modifications

Common Mistakes with node api

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable

These mistakes appear frequently in real-world GATSBY code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between createPages and onCreatePage in Gatsby?

createPages runs once during build to create new pages. onCreatePage runs every time a page is created (including by other plugins) and allows you to modify existing pages.

How do I debug Gatsby Node API functions?

Add console.log statements inside your API functions. The output appears in the terminal where you run gatsby develop or gatsby build.

Can I use async/await in Gatsby Node API?

Yes. All Gatsby Node API functions support async/await. Return a promise or use async/await for asynchronous operations like GraphQL queries.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro