next.jsmdxjs

How to obtain frontmatter (meta) from .mdx in nextjs?


I set up nextjs13 with ts support and mdx according to the docs.

Now I want to import the frontmatter in one file which was exported from another file. Is that possible?

pages/post.mdx contains

export const meta = {
  title: "some meta",
}

pages/index.tsx contains

import post from './post.mdx';
console.log(post.meta);

Output:

undefined

Expected output:

{ title: "some meta" }

Solution

  • Found the solution, while stepping over a github comment:

    Wrong:

    import post from './post.mdx';
    console.log(post.meta);
    

    Correct:

    import post, { meta } from './post.mdx';
    console.log(meta);