MDX Notes
How to get MDX installed and working with your bundler. Covers npm packages, Next.js, Vite, and webpack configs — plus fixing the errors you
MDX lets you drop React components right into your Markdown. But before you can do that, you need to install the right packages and tell your bundler how to handle .mdx files.
Let's get you set up.
Prerequisites
You need two things on your machine before we start:
Node.js
MDX needs Node.js 16 or higher. Check what you've got:
node --versionv20.11.0
If you're behind, grab the latest from nodejs.org or use nvm:
# Install nvm (Node Version Manager)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# Install the latest LTS version
nvm install --lts
nvm use --ltsPackage Manager
Pick one — npm, yarn, or pnpm. They all work fine:
# Verify your package manager
npm --version
yarn --version
pnpm --version10.2.4 1.22.19 8.14.0
💡 Tip: No strong preference? Go with pnpm. It's strict about dependencies, which saves you from weird phantom-dependency bugs down the road.Next.js Integration
Next.js has great MDX support. You've got two options depending on how your content works:
@next/mdx (MDX files as pages)
Use this when your .mdx files live in your project and you want them treated as pages or importable modules.
# npm
npm install @next/mdx @mdx-js/loader @mdx-js/react
# yarn
yarn add @next/mdx @mdx-js/loader @mdx-js/react
# pnpm
pnpm add @next/mdx @mdx-js/loader @mdx-js/reactThen update your next.config.mjs:
next-mdx-remote (Content from a CMS or database)
Use this when your MDX content doesn't live in your source code — maybe it comes from a CMS, a database, or an API.
# npm
npm install next-mdx-remote
# yarn
yarn add next-mdx-remote
# pnpm
pnpm add next-mdx-remoteHere's how you'd use it with the App Router:
💡 Tip: Using the App Router? Import fromnext-mdx-remote/rscfor server component support. On the Pages Router, use the standardnext-mdx-remoteimports withserializeandMDXRemote.
Webpack Configuration
Running a custom webpack setup without a framework? Here's what you need:
# npm
npm install @mdx-js/loader @mdx-js/mdx @mdx-js/react
# yarn
yarn add @mdx-js/loader @mdx-js/mdx @mdx-js/react
# pnpm
pnpm add @mdx-js/loader @mdx-js/mdx @mdx-js/reactAdd the MDX loader to your webpack.config.js:
Vite Configuration
Vite uses the Rollup plugin:
# npm
npm install @mdx-js/rollup @mdx-js/react
# yarn
yarn add @mdx-js/rollup @mdx-js/react
# pnpm
pnpm add @mdx-js/rollup @mdx-js/reactConfigure vite.config.js:
⚠️ Warning: That enforce: 'pre' bit is important. Without it, Vite processes files in the wrong order and you'll get cryptic compilation errors.Optional: Useful Plugins
These remark/rehype plugins are worth grabbing for most projects:
# Syntax highlighting
npm install rehype-highlight
# GitHub Flavored Markdown (tables, strikethrough, etc.)
npm install remark-gfm
# Auto-link headings
npm install rehype-slug rehype-autolink-headings
# Math/LaTeX support
npm install remark-math rehype-katexTroubleshooting Common Issues
Error: "Cannot find module '@mdx-js/react'"
Usually means it's not installed, or your lockfile is out of sync:
# Clear node_modules and reinstall
rm -rf node_modules package-lock.json
npm installError: "Unexpected token" when importing .mdx files
Your bundler doesn't know what to do with .mdx files. Double-check that the MDX loader or plugin is actually registered in your config.
Error: "React is not defined" in MDX files
MDX v2+ uses the automatic JSX runtime. If you're seeing this, tell Babel to use it:
Version Conflicts Between MDX v1 and v2/v3
Upgrading from v1? Rip out the old packages first, then install fresh:
npm uninstall @mdx-js/mdx @mdx-js/react @mdx-js/loader
npm install @mdx-js/mdx@latest @mdx-js/react@latest @mdx-js/loader@latestTypeScript Support
Add the MDX type definitions so TypeScript stops complaining:
npm install --save-dev @types/mdxThen add to your tsconfig.json:
Verifying Your Installation
Quick sanity check — create a test file and see if it compiles:
# Create a test file
echo '# Hello MDX\n\nThis is **working**!' > test.mdx# test.mdx created successfully # Your build tool should compile it without errors
If your build runs clean, you're good to go.
Summary
Here's the cheat sheet:
- Core packages:
@mdx-js/mdx(compiler) +@mdx-js/react(React glue) - Next.js:
@next/mdxfor file-based MDX,next-mdx-remotefor dynamic content - Webpack: Add
@mdx-js/loaderto your module rules - Vite: Use
@mdx-js/rollupwithenforce: 'pre' - Always match package versions — don't mix v1 and v2/v3
- Grab remark/rehype plugins for syntax highlighting, GFM tables, etc.
You're installed. Next up: creating your first MDX file.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Installing MDX - Complete Setup Guide.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this MDX Tutorial topic.
Search Terms
mdx-tutorial, mdx tutorial, mdx, tutorial, setup, installation, installing mdx - complete setup guide
Related MDX Tutorial Topics