mdx-loader
raw JSON →A batteries-included webpack loader for converting Markdown files into React components using MDX. Version 3.0.2 (latest) builds on mdx-js/mdx to provide emoji support (remark-emoji), automatic image embedding (remark-images), heading IDs (remark-slug), Prism.js syntax highlighting (rehype-prism), front matter export (gray-matter), table of contents (mdx-table-of-contents), and typographic improvements (remark-textr). Released as part of the mdx-util monorepo, it offers a zero-config experience for Webpack projects and can also be used inline with create-react-app via webpack loader syntax. Releases are infrequent; the loader uses MDX v1 and has not been updated for MDX v2, which is a breaking change. Key differentiator: all-in-one setup without needing separate remark/rehype plugins.
Common errors
error Module parse failed: Unexpected token (14:9). You may need an appropriate loader to handle this file type. ↓
error Cannot find module '@mdx-js/mdx' ↓
npm install --save-dev @mdx-js/mdx error export 'frontMatter' (imported as 'frontMatter') was not found in './doc.mdx' ↓
Warnings
breaking The package is not compatible with MDX v2. Do not use with @mdx-js/mdx@^2. ↓
deprecated The package has not been updated since 2020; consider alternatives like @mdx-js/loader for new projects. ↓
gotcha Requires babel-loader in the chain; output is ES2015 and must be transpiled for older browsers. ↓
gotcha Syntax highlighting only works if you import a Prism stylesheet manually. ↓
Install
npm install mdx-loader yarn add mdx-loader pnpm add mdx-loader Imports
- default wrong
const Component = require('./doc.mdx')correctimport Component from './doc.mdx' - frontMatter wrong
import frontMatter from './doc.mdx'correctimport { frontMatter } from './doc.mdx' - tableOfContents
import { tableOfContents } from './doc.mdx'
Quickstart
// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.mdx?$/,
use: ['babel-loader', 'mdx-loader']
}
]
}
};
// Component.js
import React from 'react';
import Doc, { frontMatter, tableOfContents } from './doc.mdx';
export default function MyPage() {
return (
<div>
<h1>{frontMatter.title}</h1>
<Doc />
<pre>{JSON.stringify(tableOfContents, null, 2)}</pre>
</div>
);
}