next-mdx-remote: Breaks with latest version of remark-gfm; unhelpful error message
I’m not really sure where to file this, but:
I just started using next-mdx-remote, and everything was working fine until I added the remark-gfm plugin. This is the entire error I was getting, with no further information:
Error: [next-mdx-remote] error compiling MDX: Cannot read properties of undefined (reading ‘inTable’)
After some trial and error, I narrowed the issue down to inline code blocks – I only get the error when they’re present.
After a bit more flailing I figured out that remark-gfm released version 4.0 a few days ago, which uses remark-parser v11, while @mdx-js/mdx uses remark-parser v10. Rolling remark-gfm back to version 3.x fixed the issue.
I’ve made a minimal repro here: https://codesandbox.io/p/github/HerbCaudill/next-mdx-remote-vs-remark-gfm
I’d assume whatever needs to be fixed is not in this library but somewhere upstream; noting the solution here in case it helps anyone.
However I would suggest revisiting the error formatter in format-mdx-error.ts. For some reason it removes the stack trace before throwing MDX formatting errors, which made this very difficult to debug. I can’t imagine what the reason for doing that would be.
About this issue
- Original URL
- State: open
- Created 9 months ago
- Reactions: 43
- Comments: 31
Commits related to this issue
- fix(mdx): switch to forked next-mdx-remote supporting MDXv3 See hashicorp/next-mdx-remote#403 and hashicorp/next-mdx-remote#419. — committed to sabertazimi/blog by sabertazimi 5 months ago
- fix(deps): update remark (major) (#1180) * fix(deps): update remark * fix(mdx): switch to forked next-mdx-remote supporting MDXv3 See hashicorp/next-mdx-remote#403 and hashicorp/next-mdx-remot... — committed to sabertazimi/blog by renovate[bot] 5 months ago
- feat: added github flavored markdown - https://github.com/hashicorp/next-mdx-remote/issues/403 - to deal with this issue, remark-gfm is downgraded to 3.0.1 — committed to ntucpc/ntucpc-guide-frontend by deleted user 4 months ago
I downgraded
remark-gfmfrom version4.0.1to version3.0.1and the error was goneI can confirm this solution works, good catch! The error appeared for me no matter what GFM component I used, although I first noticed after trying to add tables.
Another update from @dstaley: Canary releases of v5 are now available (check out the first comment by the bot to get the latest published canary version):
Downgrading
remark-gfmto version 3 solves the problem on my end.(For reference, I’m using Storybook with gfm in my MDX files and am following their official guide for adding gfm support)
FYI this issue is already solved on all upstream libraries. Problem is,
next-mdx-remoteneeds to update their version of said libraries, otherwise some will be using older ones whilst other newer ones and that will well… break.@paigen11 open the “Published 1 packages” on this comment:
Inside you’ll find the version that you can add to your
package.jsonfile fornext-mdx-remote(and then runnpm install- or whatever your package manager install command is)I also get this error if there is a table in the markdown.
The solution to use v3 works for me for tables and back-ticks.
I confirm having the same issue, a simple MDX with an inline code triggers the error message.
@snypiie if you’re using
next-mdx-remoteinside of agetStaticPropscall, you’ll need to do something like this to use the remark plugin with it.The part to focus on is the
mdxSourcefunction where{ remarkPlugins: [gfm] }is included. That’s what you need to add to make tables format correctly withnext-mdx-remote@paigen11 I’m aiming to release v5 sometime this month, and hearing that it fixes your issue is a great thing to hear! As for
mdx-components.tsx, it’s not something we have specific support for. You could certainly define your components there and import them from there to pass toMDXRemotethough!@jbukuts Definitely give the canary release of v5 a shot! Should be much smoother sailing.
As an additional note for anyone trying to use
@next/mdxor any other tool alongsidenext-mdx-remote. Most of the issues relate to conflicts in dependency versions.For instance, when I tried using
@next/mdxalongside it I hade to bump my@mdx-js/loaderand@mdx-js/reactversions down to match thenext-mdx-remotepackage.json.TL;DR Play package.json musical chairs with dependencies. Hopefully v5 fixes the chaos.
Bumping it as we still need a proper fix
I also confirm rolling back to v3.0.1 works, still we need a fix for the v4 as well 😕/
I’m using next@14.0.4 and this works for me. Thanks!
The problem is in https://github.com/syntax-tree/mdast-util-gfm-table/
lib/index
line 79 //this.setdata.inTable = true this.setData(‘inTable’, true)
and line 89 //this.data.inTable = undefined this.setData(‘inTable’)
the problem is because this.data is undefined while trying to update the prop inTable
this is just a fix, but it breaks the build, apparently ‘CompileContext’ is supposed to initialize this.data, but it isn’t.
Error: lib/index.js(79,8): error TS2339: Property ‘setData’ does not exist on type ‘CompileContext’. error while trying to do a pull request
Can someone give me feedback about ‘CompileContext’?
Is this somewhat going to be addressed by the maintainers here? As the last release was in March.