Oops! Looks like there's a type error in module "*.mdx" - it seems that it doesn't have the exported member "metadata". Maybe try using "import metadata from "*.mdx"" instead?

I am attempting to extract metadata from an mdx file.
I have followed the guidelines outlined in NextJS Markdown Frontmatter, but encountered build errors.

It is important to note that I am unable to utilize fs.

Code Section

Page.tsx File


import ContentPost from "@/components/post.mdx"


const PostPage = () => {
  

  return (
    <div>
          <div className="  mb-8 md:mb-24 text-center">
            <h1 className="text-3xl">Title</h1>
            <p className="text-slate-400 mt-2">date</p>
          </div>

          <article className="prose mb-52 prose-emerald md:prose-lg lg:prose-xl dark:prose-invert">
            <ContentPost />
          </article>
    </div>
  );
};

export default PostPage;

*.mdx File

---
    title: "Title",
    subtitle: "Sub",
    date: "2024-3-18",
    coverImage: "",
---

# Contents text

mdx-components.tsx File

import type { MDXComponents } from 'mdx/types'
 
export function useMDXComponents(components: MDXComponents): MDXComponents {
  return {
    ...components,
  }
}

next.config.mjs File

import nextMDX from '@next/mdx';
const withMDX = nextMDX({
    extension:/\.mdx?$/,
    options:{
        remarkPlugins:[],
        rehypePlugins:[],
    }
})


/** @type {import('next').NextConfig} */
const nextConfig = {
    pageExtensions: ['js', 'jsx', 'mdx', 'md', 'ts', 'tsx'],
    images:{
        unoptimized: true,
        remotePatterns:[
        {
            protocol: "https",
            hostname: "img.youtube.com",
        },
    ]
    }
};

export default withMDX(nextConfig);

I Also Attempted The Following

export const metadata = {
    title: "Title",
    subtitle: "Sub",
    date: "2024-3-18",
    coverImage: "",
}
 
# My MDX page

In this scenario, I successfully retrieved the metadata from the .mdx file and it ran smoothly when using npm run dev. However, a build error occurred as follows:


Type error: Module '"*.mdx"' has no exported member 'metadata'. Did you mean to use 'import metadata from "*.mdx"' instead?

  4 | import Link from "next/link";
  5 |
> 6 | import ContentPost, { metadata } from "@/components/post.mdx";
    |                       ^
  7 |
  8 |
  9 | const PostPage = () => {


Error: An unexpected error has occurred.

Answer №1

I managed to find a solution on my own.
However, it didn't fully address my initial question.

Initially, I was unfamiliar with frontmatter and unable to utilize the frontmatter API.
Once I grasped that concept, I came across the necessary function, matter.read("md_path"). Additionally, I had to convert the file from *.mdx to *.md.

To tackle this issue, I made use of two primary APIs:

import Markdown from "markdown-to-jsx"
import matter from "gray-matter";

const PostPage = () => {
  const postObj =matter.read("public/post-contents/post.md")
  const metadata = postObj.data

  return (
    <div>
          <div className="  mb-8 md:mb-24 text-center">
            <h1 className="text-3xl">{metadata.title}</h1>
            <p className="text-slate-400 mt-2">{metadata.date}</p>
          </div>
          <article className="prose mb-52 prose-emerald md:prose-lg lg:prose-xl dark:prose-invert">
            <Markdown>{postObj.content}</Markdown>
          </article>
    </div>
  );
};

export default PostPage;

I'm still unsure of how to handle a *.mdx file in this context.
If there were no build error, it would have been a straightforward approach to implement.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When making an XMLHTTPRequest, Chrome often responds with an "Aw, snap"

In the development of my server-based multiplayer game using three.js, I have set up the server to operate as follows: game->Server2.php->Server.txt game->Server.php->Server.txt->game The process flow is as follows: 1.) The game is gener ...

Strange occurrences observed in the functionality of Angular Material Version 16

Encountered a strange bug recently. Whenever the page height exceeds the viewport due to mat-form-fields, I'm facing an issue where some elements, particularly those from Angular Material, fail to load. Here's a GIF demonstrating the problem: GI ...

Using Angular NgUpgrade to inject an AngularJS service into an Angular service results in an error message stating: Unhandled Promise rejection: Cannot read property 'get' of undefined; Zone:

I have noticed several similar issues on this platform, but none of the solutions seem to work for me. My understanding is that because our Ng2App is bootstrapped first, it does not have a reference to $injector yet. Consequently, when I attempt to use it ...

What is preventing Google Chrome from locating my app.js file?

Just embarking on my journey to learn Nodejs through a basic app. Strangely, Google Chrome is unable to locate my app.js file in the /assets/js.app directory. https://i.sstatic.net/Eu8FG.png Reviewing the paths I've configured, it seems everything i ...

The Server Components render encountered a glitch

Screenshot of the errorI am encountering a strange error only in the production environment. The lack of additional information leads me to believe it may be due to security measures put in place for production. Unfortunately, I have been unable to repli ...

React: Struggling to retrieve specific elements from an array stored in component state

For a fun side project, I've taken on the challenge of creating a miniature Pokedex app using React. import React, { Component} from 'react'; import './App.css'; import Card from './components/card/Card.component'; clas ...

Is there a way to retrieve the dynamically generated text content of an element using document.write?

I am encountering an issue similar to the following: <div id="myDiv"><script>document.write('SOMETHING');</script></div> My goal is to extract the content inside the script tag, which in this case is "SOMETHING" ...

Angular is a powerful framework that enables the creation of dynamic user interfaces. One of its many

Looking to implement a Material table with expandable rows in Angular. table-tree.html <table mat-table [dataSource]="dataSource" multiTemplateDataRows class="mat-elevation-z8" > <ng-container matColumnDef="{{co ...

Retrieve the user information from Auth0 within the NestJS application

I am currently working on implementing Auth0 authorization in NestJS, but I am unsure of how to retrieve the user's data within the callback URL handler. In a normal express function, this issue could be resolved using the following code. The passpor ...

Implemented rounded corners to the bar chart background using Recharts

I have been experimenting with creating a customized bar chart that includes border radius for the bars. While I was successful in adding border radius to the bars themselves, I am struggling to add border radius to the background surrounding the bars. Any ...

There seems to be an issue with the hidden field value not being properly set within the

I created a function called getConvertionValue. Inside this function, I make an ajax call to the getCurrencyConvertion function in the controller. function getConvertionValue(from, to) { if (from != to) { $.ajax({ url: base_url + 'admin/o ...

Update the canvas box's color when you interact with it by clicking inside

I'm in the process of developing a reservation system and I'm looking to implement a feature where the color of a Canvas changes when clicked. The goal is for the color to change back to its original state when clicked again. Snippet from my res ...

Tips on extracting image data from FilePond

I'm facing a challenge while trying to upload image files to my nextjs app with the intention of storing them in GCS. The issue lies with handling the image form data. To tackle this, I've implemented the use of FilePond on the client side to fac ...

Leveraging Angular Observables for seamless data sharing across components

As I embark on developing my very first Angular app, I have encountered a challenge with filtering a list of book objects based on their gender attribute. The issue lies in sharing data between components – specifically the filteredData variable and the ...

Having trouble with Visual Studio 2015 not compiling TypeScript within an ASP.NET Core project?

Seeking assistance with my Angular app development setup in VS2015. Even though it is recognized as a TypeScript Virtual Project, I am facing issues getting the transpiled files into the wwwroot folder within my ASP.NET Core project. Here's an overvie ...

Expanding a TypeScript type by creating an alias for a property

I am working on defining a type that allows its properties to be "aliased" with another name. type TTitle: string; type Data<SomethingHere> = { id: string, title: TTitle, owner: TPerson, } type ExtendedData = Data<{cardTitle: "title&qu ...

Unable to Retrieve Response from jQuery AJAX in a Function

Having some trouble with a jQuery AJAX function that is supposed to retrieve data from a file. I can't seem to get the function to return the actual value. Any suggestions? $(document).ready(function () { function fetchData() { ...

What are the steps to update your profile picture using Angular?

In my Angular 6 application, I am implementing an image upload feature with the following code: Html: <img [src]="url ? url : 'https://www.w3schools.com/howto/img_avatar.png'"> <br/> <input type='file' (change)="onSelec ...

Is there a specific jest matcher available for comparing Array<Objects>?

I'm currently testing the equality of two arrays of objects and have found that the toEqual matcher in Jest only works for arrays of strings. Is there another matcher available in Jest that can handle this condition? Please refrain from marking this a ...

Contrast between sourcing a file from the current directory versus from the node_modules folder

Why does the typescript compiler accept importing from a JS file in the local directory but raises an error when importing from node_modules? Code: import { t2 } from "./t1.js" t2.hello(); import { mat4 } from "./node_modules/gl-matrix/esm ...