question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to reference frontmatter fields inside MDX

See original GitHub issue

Description

Documentation states https://www.gatsbyjs.org/docs/mdx/writing-pages/ that Frontmatter is also available in props.pageContext.frontmatter and can be accessed in blocks of JSX in your MDX document:

When I try to read my frontmatter inside mdx I get an error: Cannot read property 'frontmatter' of undefined

System: OS: macOS 10.15.2 CPU: (16) x64 Intel® Core™ i9-9980HK CPU @ 2.40GHz Shell: 5.7.1 - /bin/zsh Binaries: Node: 12.14.1 - /usr/local/bin/node npm: 6.13.4 - /usr/local/bin/npm Languages: Python: 2.7.16 - /usr/bin/python npmPackages: gatsby: ^2.15.15 => 2.15.15 gatsby-image: ^2.2.19 => 2.2.19 gatsby-plugin-alias-imports: ^1.0.5 => 1.0.5 gatsby-plugin-catch-links: ^2.1.9 => 2.1.9 gatsby-plugin-facebook-pixel: ^1.0.3 => 1.0.3 gatsby-plugin-google-analytics: ^2.1.16 => 2.1.16 gatsby-plugin-google-gtag: ^1.1.8 => 1.1.8 gatsby-plugin-mdx: ^1.0.41 => 1.0.41 gatsby-plugin-react-helmet: ^3.1.7 => 3.1.7 gatsby-plugin-sharp: ^2.2.22 => 2.2.22 gatsby-plugin-sitemap: ^2.2.12 => 2.2.12 gatsby-plugin-styled-components: ^3.1.5 => 3.1.5 gatsby-remark-autolink-headers: ^2.1.10 => 2.1.10 gatsby-remark-copy-linked-files: ^2.1.19 => 2.1.19 gatsby-remark-images: ^3.1.22 => 3.1.22 gatsby-source-filesystem: ^2.1.22 => 2.1.22 gatsby-transformer-json: ^2.2.25 => 2.2.25 gatsby-transformer-remark: ^2.6.22 => 2.6.22 gatsby-transformer-sharp: ^2.2.14 => 2.2.14 npmGlobalPackages: gatsby-cli: 2.8.29

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
AAverincommented, Feb 7, 2020

Maybe docs need to be updated? I am using MDXRenderer and nothing was accessible until I have manually passed a parameter

<MDXRenderer frontmatter={mdx.frontmatter}>{mdx.body}</MDXRenderer>
0reactions
lambertbradycommented, Sep 18, 2020

I’ve run into the same problem - on top of that, passing frontmatter as a prop to MDXRenderer breaks the build when trying to use it, even though gatsby develop works just fine. Here’s my usage:

In my post component: <MDXRenderer frontmatter={post.frontmatter}>{post.body}</MDXRenderer>

In my index.mdx file:

---
title: Hello World
---

<p>{props.frontmatter.title}</p>

Running gatsby develop displays the frontmatter output as expected. Running gatsby build throws an error:

gatsby-plugin-mdx
TypeError: Cannot read property 'title' of undefined
Read more comments on GitHub >

github_iconTop Results From Across the Web

gatsby-plugin-image and GraphQL don't recognize my image ...
It simply sees the image: key in the frontmatter as a string and never gives it the childImageSharp fields. Attempting to force the...
Read more >
Adding MDX Pages | Gatsby
In order to create pages from the sourced MDX files, you need to construct a query that finds all MDX nodes and pulls...
Read more >
Frontmatter - MDX
Frontmatter. This guide explores how to support YAML frontmatter in MDX. MDX supports standard markdown syntax (CommonMark).
Read more >
plugin-content-docs | Docusaurus
{md,mdx}'], Array of glob patterns matching Markdown files to be built ... The version navigated to in priority and displayed by default for ......
Read more >
gatsby-mdx is depreciated, use gatsby-plugin-mdx instead
There was an error in your GraphQL query: – Unknown field 'code' on type 'Mdx'. To fix this you have to do: query...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found