Gatsby Node Helpers
The `gatsby-node-helpers` package provides a set of utility functions specifically designed to simplify and streamline the process of creating Gatsby nodes within source plugins. Its current stable version is 1.2.1. While not adhering to a strict time-based release cadence, the project actively publishes updates, with recent releases addressing feature enhancements and minor adjustments. It differentiates itself by abstracting away the complexities of Gatsby's internal node requirements, such as automatically generating `contentDigest`, handling Gatsby's reserved field conflicts through intelligent namespacing, and providing compliant functions for generating type names and unique IDs. This significantly streamlines the development of Gatsby source plugins by ensuring created nodes adhere to Gatsby's internal data model, reducing boilerplate and common errors associated with manual node creation in `gatsby-node.js` files.
Common errors
-
TypeError: createNode is not a function
cause `createNode` was not properly destructured from `actions` in Gatsby's Node APIs.fixEnsure `const { actions } = gatsbyArgs; const { createNode } = actions;` or directly `gatsbyArgs.actions.createNode(node)`. -
ReferenceError: createNodeHelpers is not defined
cause `createNodeHelpers` was not imported or was imported incorrectly (e.g., CommonJS `require` in an ESM context).fixUse `import { createNodeHelpers } from 'gatsby-node-helpers'` at the top of your `gatsby-node.js` or `gatsby-node.ts` file. -
Cannot find name 'GatsbyNode' / Property 'sourceNodes' does not exist on type 'GatsbyNode'
cause Missing or incorrect Gatsby TypeScript types, or an outdated Gatsby version not matching the type definitions.fixInstall `@types/gatsby` (`npm install --save-dev @types/gatsby`) and ensure your `gatsby` peer dependency meets the `gatsby-node-helpers` requirements (e.g., `>=2.29`).
Warnings
- breaking The `generateNodeId` utility (used internally by `createNodeFactory`) now camelCases the node's type. This changes the generated ID format for node types that previously used underscores or kebab-case.
- gotcha When using `createNodeFactory` with an async middleware function, ensure you `await` the result of the factory call before passing it to `createNode`.
- gotcha The `createNodeHelpers` function requires Gatsby's `createNodeId` and `createContentDigest` functions from the `gatsbyArgs` object passed to Gatsby's Node APIs (like `sourceNodes`).
Install
-
npm install gatsby-node-helpers -
yarn add gatsby-node-helpers -
pnpm add gatsby-node-helpers
Imports
- createNodeHelpers
const { createNodeHelpers } = require('gatsby-node-helpers')import { createNodeHelpers } from 'gatsby-node-helpers' - createNodeFactory
import { createNodeFactory } from 'gatsby-node-helpers'const ProductNode = nodeHelpers.createNodeFactory('Product') - GatsbyNodeHelpers
import { GatsbyNodeHelpers } from 'gatsby-node-helpers'import type { GatsbyNodeHelpers } from 'gatsby-node-helpers'
Quickstart
import * as gatsby from 'gatsby'
import { createNodeHelpers } from 'gatsby-node-helpers'
interface MyProduct {
id: string;
name: string;
price: number;
}
// Simulate fetching product data
async function getAllProducts(): Promise<MyProduct[]> {
return [
{ id: 'p1', name: 'Product A', price: 29.99 },
{ id: 'p2', name: 'Product B', price: 49.99 }
]
}
export const sourceNodes: gatsby.GatsbyNode['sourceNodes'] = async (
gatsbyArgs: gatsby.SourceNodesArgs,
) => {
const { actions, createNodeId, createContentDigest } = gatsbyArgs
const { createNode } = actions
const nodeHelpers = createNodeHelpers({
typePrefix: 'MyPlugin',
createNodeId,
createContentDigest,
})
const ProductNode = nodeHelpers.createNodeFactory<MyProduct>('Product')
const products = await getAllProducts()
for (const product of products) {
const node = await ProductNode(product)
// `node` now contains all the fields required by `createNode`
// (e.g., id, parent, children, internal.contentDigest, internal.type, etc.)
createNode(node)
}
}