How should mdx-js/loader v2 works with CRA? #1870
-
Hello, I'm having a problem using MDX with create-react-app, i tried i every possible way for hours but without success.. The problem is this: following the documentation i installed the @mdx-js/loader, and as the docs say: 'There is no need to configure it'. But when i create the example file, (i tried with the same example from the docs, but also other examples), React throws an exception telling me something like this:
With the debugger i can see that React is trying to call createElement with the mdx file path as a parameter.
I noticed also, that the result does not change if i omit the inline babel import of mdx-js/loader. The error is the same:
There are the version i'm using for these experiments:
PS: After some trials, i decided to try it also on Next.js, and following the docs, it works perfectly, so the problem should be only in CRA. Thank you guys. |
Beta Was this translation helpful? Give feedback.
Replies: 14 comments 33 replies
-
Could definitely be that CRA is not supported currently. Did you try their latest, v5? |
Beta Was this translation helpful? Give feedback.
-
I'm also experiencing the same issue (copy of #1871 (comment)). package.json: {
"private": true,
"dependencies": {
"@mdx-js/loader": "^2.0.0-rc.2",
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/react": "^12.1.2",
"@testing-library/user-event": "^13.5.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0",
"web-vitals": "^2.1.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
} src/content.mdx: # Hello, world!
This is **markdown** with <span style={{color: "red"}}>JSX</span>: MDX! src/App.js: /* eslint-disable import/no-webpack-loader-syntax */
import Content from '!@mdx-js/loader!./content.mdx'
export default function App() {
return <Content />
} If I try to do it an error is thrown in the console, something like this: Uncaught DOMException: Failed to execute 'createElement' on 'Document': The tag name provided ('/static/media/content.152fde8da01171ae4224.mdx') is not a valid name.
at createElement (http://localhost:3000/static/js/bundle.js:16306:38)
at createInstance (http://localhost:3000/static/js/bundle.js:17493:24)
at completeWork (http://localhost:3000/static/js/bundle.js:26619:32)
at completeUnitOfWork (http://localhost:3000/static/js/bundle.js:29924:20)
at performUnitOfWork (http://localhost:3000/static/js/bundle.js:29896:9)
at workLoopSync (http://localhost:3000/static/js/bundle.js:29822:9)
at renderRootSync (http://localhost:3000/static/js/bundle.js:29788:11)
at performSyncWorkOnRoot (http://localhost:3000/static/js/bundle.js:29405:22)
at scheduleUpdateOnFiber (http://localhost:3000/static/js/bundle.js:28993:11)
at updateContainer (http://localhost:3000/static/js/bundle.js:32530:7) |
Beta Was this translation helpful? Give feedback.
-
Hi! I'm experiencing the same exact issue on a fresh CRA. I wanted to know if this has been fixed or someone found a workaround, since i'm following the getting started guides literally without success. |
Beta Was this translation helpful? Give feedback.
-
I was able to get a working configuration by downgrading @mdx-js/loader to 1.6.22 and react-scripts to 4.0.3. Delete node_modules and package-lock.json in your project, update your dependency versions in package.json to:
Then run npm install to re-install your dependencies. When importing .mdx files using mdx-js/loader, you may get an error parsing the resulting JSX data. I was able to resolve this by using babel-loader as well
You may need to create a .babelrc file in your root directory with the following presets:
|
Beta Was this translation helpful? Give feedback.
-
Hi, I am facing the same issue, but i don't want to downgrade my libraries to prevent dependencies vulnerabilties. There is a solution to use mdx/loader with create-react-app like describe in the mdx documentation ? how i can compile my .mdx file to execute 'createElement' like ? Than you for your help, |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm experiencing the same problem, does someone found out any fix or workaround for it? Thanks' |
Beta Was this translation helpful? Give feedback.
-
I haven't yet found a fix for this, but I want to document some of my findings:
Relevant links: I confirmed this by changing this line locally and running
to
Once I did that, the file contents were picked up as I expected. I'm not sure if this is a bug in MDX, CRA, or Webpack. As I understand it, using the syntax |
Beta Was this translation helpful? Give feedback.
-
Any updates on this one? I have been trying to configure React with MDX for two days now but nothing seems to work. I have tried using react-app-rewired and config-overrides.js but it fails and gives Following thread. |
Beta Was this translation helpful? Give feedback.
-
I opened an issue in CRA here: The fix from @brian-petersen above (#1870 (comment)) works for me locally. |
Beta Was this translation helpful? Give feedback.
-
Hey @zingbretsen did you receive any update on merging the file ? This was really help, it works fine my local but not through pipeline. |
Beta Was this translation helpful? Give feedback.
-
Thanks @brian-petersen – After hours of frustration your #1870 (comment) saved my day … OMG: late evening already :D This discussion is unfortunately hard to find when searching for Anyway: Not only |
Beta Was this translation helpful? Give feedback.
-
Can anyone help me out? I have the similar problem but @brian-petersen method doesn't fix it for me. I have added {
test: /\.mdx?$/,
use: [
{
loader: '@mdx-js/loader',
/** @type {import('@mdx-js/loader').Options} */
options: {}
}
]
} in the rules of webpack.config.js and also changed the line mentioned in his comment and added "|mdx" I have installed @mdx-js/loader, @types/mdx and @mdx-js/react (not relevant) Any ideas what might be wrong? |
Beta Was this translation helpful? Give feedback.
-
Finally: I've found an elegant workaround :) —using https://github.com/gsoft-inc/craco & the following const { addAfterLoader, loaderByName } = require('@craco/craco')
module.exports = {
webpack: {
configure: (webpackConfig) => {
addAfterLoader(webpackConfig, loaderByName('babel-loader'), {
test: /\.(md|mdx)$/,
loader: require.resolve('@mdx-js/loader')
})
return webpackConfig
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Summarizing the current status. The best course of action is to migrate to a build tool which is maintained.
You can find documentation on how MDX integrates with vite here: https://mdxjs.com/docs/getting-started/#vite If you'd like to add server side rendering support while migrating, React has some recommendations https://react.dev/learn/start-a-new-react-project (all of which also have MDX integrations) If you are unable to migrate to a actively maintained build tool, see https://github.com/orgs/mdx-js/discussions/1870#discussioncomment-2530611 for a workaround for Create React App using CRACO. |
Beta Was this translation helpful? Give feedback.
Summarizing the current status.
Create React App is deprecated and is no longer actively being maintained. reactjs/react.dev#5487 (comment)
The best course of action is to migrate to a build tool which is maintained.
Vite and Vitest have similar low configuration setup and extensive features set as Create React App had.
Some resources showing how to migrate: