Skip to content

Gatsby MDX Error Fix

DodaTech Updated 2026-06-24 1 min read

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

The Problem

Error: Module not found: Error: Can't resolve '@mdx-js/react' in '/path/to/project'

Gatsby MDX requires specific packages to compile .mdx files into React components.

Wrong

Installing only gatsby-plugin-mdx without its peer dependencies:

npm install gatsby-plugin-mdx

Missing @mdx-js/mdx and @mdx-js/react causes compilation errors.

npm install gatsby-plugin-mdx @mdx-js/mdx @mdx-js/react

Configure in gatsby-config.js:

module.exports = {
  plugins: [
    {
      resolve: 'gatsby-plugin-mdx',
      options: {
        extensions: ['.mdx', '.md'],
      },
    },
    'gatsby-source-filesystem',
  ],
}

Create a page content/post.mdx:

---
title: Hello MDX
---

# Hello MDX

<Button>Click me</Button>

export const Button = ({ children }) => (
  <button style={{ color: 'blue' }}>{children}</button>
)

Output: MDX file renders as a React component with the Button inlined.

Prevention

  • Install all peer dependencies: @mdx-js/mdx, @mdx-js/react, and gatsby-plugin-mdx
  • Add gatsby-plugin-mdx to your plugins array in config
  • Set the extensions option to handle .mdx and .md files

Common Mistakes with mdx error

  1. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  2. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  3. Misunderstanding that String is [Char] with poor performance for large text operations

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 MDX and regular Markdown in Gatsby?

MDX allows you to use JSX components inside Markdown content. Regular Markdown only supports HTML. MDX files use the .mdx extension.

How do I use Gatsby components inside MDX?

Import components at the top of your MDX file or use the mdxProvider in gatsby-browser.js to make components globally available.

Why does my MDX file show raw JSX instead of rendered HTML?

The file extension must be .mdx (not .md), and gatsby-plugin-mdx must be configured to handle that extension. Check the extensions option in your plugin config.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro