Fix: Cannot find module 'MyComponent.mdx' or its corresponding type declarations in React
If you are trying to import .mdx files with Typescript in your React project, you might be getting an error looking like this:
Fix: Cannot find module 'MyComponent.mdx' or its corresponding type declarations in ReactHere's how to fix it:
First, install @types/mdx:
pnpm i -D @types/mdxThen, inside your project, add a types folder with a mdx.d.ts file (e.g. ./src/types/mdx.d.ts), and paste this in the file:
declare module '*.mdx' {
let MDXComponent: (props: any) => JSX.Element;
export default MDXComponent;
}Now open your tsconfig.json file, and do these two things:
Add
"mdx"inside"types":
{
"compilerOptions": {
...
"types": ["react/next", "react-dom/next", "mdx"]
},
...
}Make sure the path of the
mdx.d.tsfile is included. If you added it inside thesrcfolder, this should be enough:
{
...
"include": ["src"],
...
}It should work now. Good luck!