Gatsby Transformer JavaScript Frontmatter
This is a Gatsby transformer plugin designed to extract metadata, commonly known as "frontmatter," directly from JavaScript, TypeScript, JSX, or TSX files within a Gatsby project. It operates by statically analyzing `exports.frontmatter` or `export const frontmatter` declarations using `@babel/parser` and `@babel/traverse`. The current stable version, `5.16.0`, aligns with the Gatsby v5 ecosystem. Its release cadence is tied to the main Gatsby monorepo, receiving updates alongside Gatsby core to ensure compatibility with new Node.js versions (e.g., Node.js 24 support in `5.16.0`) and address security vulnerabilities. Its key differentiator is enabling developers to define content metadata directly within their component or data files using standard JavaScript exports, providing a unified location for code and content configuration, distinct from traditional Markdown-based frontmatter solutions.
Common errors
-
WebpackError: GraphQL Error Field 'allJavascriptFrontmatter' doesn't exist on type 'Query'
cause The `gatsby-transformer-javascript-frontmatter` plugin or `gatsby-source-filesystem` is not correctly configured in `gatsby-config.js`, or `gatsby-source-filesystem` is not pointing to directories containing JavaScript files with `frontmatter` exports.fixVerify that `gatsby-transformer-javascript-frontmatter` is listed in your `plugins` array in `gatsby-config.js` and that `gatsby-source-filesystem` is configured to include the paths to your JavaScript/TypeScript files. -
Error: Cannot find module 'gatsby'
cause Missing peer dependency for Gatsby, which is required by this transformer plugin.fixInstall `gatsby` and `gatsby-source-filesystem` at version `^5.0.0` or higher: `npm install gatsby gatsby-source-filesystem` or `yarn add gatsby gatsby-source-filesystem`. -
The 'frontmatter' field in your GraphQL query returns null or expected fields are missing.
cause The frontmatter was not correctly parsed, possibly due to incorrect export syntax in the source file or dynamic content that could not be statically analyzed.fixCheck the source JavaScript/TypeScript file to ensure `export const frontmatter = { ... }` or `exports.frontmatter = { ... }` is used and that the content is statically analyzable. The GraphQL query also exposes an `error` field you can query to get detailed parsing errors.
Warnings
- breaking Gatsby v5 and this plugin require Node.js version 18.0.0 or higher. Using older Node.js versions (e.g., Node 16) will lead to build failures or runtime errors. Ensure your development environment and deployment targets meet the Node.js requirements specified in the package's `engines` field.
- breaking This transformer plugin is part of the Gatsby v5 ecosystem. It requires peer dependencies `gatsby` and `gatsby-source-filesystem` to be at least version `5.0.0`. Installing it with Gatsby v4 or earlier will result in dependency resolution issues and functional breakage.
- gotcha The plugin relies on static analysis of JavaScript/TypeScript files using Babel. Dynamically generated `frontmatter` exports, or those involving complex runtime logic, may not be correctly parsed or extracted by the transformer.
- gotcha The plugin specifically looks for an export named `frontmatter`. Using `export default { title: '...' }` or `module.exports = { title: '...' }` will prevent the frontmatter from being detected and queried via GraphQL.
Install
-
npm install gatsby-transformer-javascript-frontmatter -
yarn add gatsby-transformer-javascript-frontmatter -
pnpm add gatsby-transformer-javascript-frontmatter
Imports
- "gatsby-transformer-javascript-frontmatter"
import { transformer } from 'gatsby-transformer-javascript-frontmatter';// in gatsby-config.js module.exports = { plugins: [ { resolve: `gatsby-source-filesystem`, options: { name: `pages`, path: `${__dirname}/src/pages/`, }, }, "gatsby-transformer-javascript-frontmatter", ], }; - frontmatter (Named ES Export)
export default { frontmatter: { title: "My Page Title" } };export const frontmatter = { title: "My Page Title", date: "2023-01-15" }; - frontmatter (CommonJS Export)
module.exports = { title: "Another Page" };exports.frontmatter = { title: "Another Page", category: "development" };
Quickstart
module.exports = {
plugins: [
{
resolve: `gatsby-source-filesystem`,
options: {
name: `pages`,
path: `${__dirname}/src/pages/`,
},
},
"gatsby-transformer-javascript-frontmatter",
],
}
// Example content file: src/pages/my-post.js
import React from "react"
export const frontmatter = {
title: "Choropleth on d3v4",
written: "2017-05-04",
layoutType: "post",
path: "choropleth-on-d3v4",
category: "data science",
description: "Things about the choropleth.",
}
export default function MyComponent() {
return <div>My Post Content</div>
}