Unable to Change MDX Export Format in TypeScript and Next.js

I created a unique plugin that converts frontmatter in MDX files to a constant export called frontMatter. Let's take the file a.mdx as an example:

---
title: "Some Title"
---

# Content

When this file is imported into a Next.js Page directly, it gets transformed at compile-time like this:

# Content

export const frontMatter = {
    title: "Some Title"
}

To import in a Typescript & Next.js page file, I use the following syntax:

import AComponent, { frontMatter } from "a.mdx"

The custom export of frontMatter works fine. However, there are issues with the Typescript compiler not recognizing this custom export unless explicitly exported from the file. Here is my current definition in tsconfig:

declare module '*.mdx' {
    import { MDXProps } from 'mdx/types'
    const MDXComponent: (props: MDXProps) => JSX.Element
    const frontMatter: { [key: string]: any }
    export { frontMatter }
    export default MDXComponent
}

This approach doesn't work as expected. Interestingly, if I declare another module within the same file, the import works perfectly:

declare module '*.myFile' {
   export const value
}

An error message that I encounter states:

Module 'index.mdx' has no exported member 'frontMatter'. Did you mean to use 'import frontMatter from "index.mdx"' instead?

It appears that there might be something conflicting with the mdx definitions despite trying multiple solutions. Below is a snippet from my tsconfig.json file:

{
    "compilerOptions": {
        "lib": ["dom", "dom.iterable", "esnext"],
        "allowJs": true,
        "skipLibCheck": true,
        "strict": true,
        "noEmit": true,
        "esModuleInterop": true,
        "module": "esnext",
        "moduleResolution": "bundler",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "jsx": "preserve",
        "incremental": true,
        "plugins": [
            {
                "name": "next"
            }
        ],
        "paths": {
            ... Some remappings here
        }
    },
    "include": [
        "next-env.d.ts",
        "**/*.ts",
        "**/*.tsx",
        "**/*.mdx",
        ".next/types/**/*.ts",
        "THE MODULE DECLARATION FILE"
    ],
    "exclude": ["node_modules"]
}

Answer №1

My issue was simply a matter of not understanding how to properly configure the MDX plugin in VSCode. To resolve this, add the following code snippet to your tsconfig.json file and restart the Typescript server. This will eliminate any errors caused by the extension:

"compilerOptions": { ... },
"mdx": {
    "plugins": [
        "remark-frontmatter",
        "remark-frontmatter-mdx",
    ]
}

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

Tips for safely storing data in local storage using Angular 5

How can I securely store data in local storage, such as encrypting and decrypting the local storage data to prevent manipulation by other users? If it's not possible with local storage, what are alternative methods for client-side data storage? I&apo ...

Implementing a defined string constant with a specific type in Typescript

Consider the following predefined object: const obj = { ONE: 'ONE', TWO: 'TWO' }; If I attempt to assign the constant obj.ONE to a new type like this: type TOne = obj.ONE An error message is displayed stating: Cannot find names ...

Guide: Implementing service utilization within a controller using Express and Typescript

This specific piece of TypeScript code is causing me some trouble. I'm attempting to utilize a service to retrieve data from a database, but unfortunately, I keep encountering the following error message: Cannot read property 'populationService&a ...

How to capture and log request and response data when using the HttpService in NestJS?

Is there a way to log requests, responses, and errors using the HttpService within the HttpModule? In the past, I have used Interceptors with AXIOS. While HttpService wraps axios, I'm having trouble adding interceptors. There doesn't seem to be ...

The pipe operator in RxJS is never executed in the Observable fork

I am attempting to execute and retrieve values from an array of observables (each obtained from literalsService) using a pipe. Below is the code snippet: translateLiterals() { const literalsToTranslate: string[] = [ 'certificate_title', ...

What steps can be taken to ensure that all object properties become reactive?

Let's dive into this simplified scenario: interface Pup { name: string; age: number; } const puppy: Pup = { name: 'Rex', age: 3, }; The goal here is to establish a reactive link for each attribute within the puppy object. The usua ...

How do I ensure my Instrument Cards maintain dynamic sizing while also defining a minimum card size with React-Bootstrap?

Let's talk about my current situation This is how my cards are currently set up: https://i.sstatic.net/M3XQ8.png Looks good, right? However, when you adjust the screen size, it looks like this: https://i.sstatic.net/eiESt.png Not so great, especi ...

Struggling to retrieve dataset from Bootstrap 5 form while using Angular

With the combination of Angular and Bootstrap 5, I have crafted this unique HTML form: <div class="mb-3"> <label for="genreName"> Type name</label> <div *ngIf="!enterTheGenre" class="form-group&qu ...

NextJS version 13.3.4 unable to locate the cache

I'm attempting to revalidate tags in a NodeJS runtime using the instructions provided here. However, I encountered an issue where the next/cache package was not installed after running 'npm i' on NextJS version 13.3.4. import { NextRequest, ...

Issue with Angular 6 where data is not binding to the UI on initialization

I am struggling with binding dynamic data to the UI and Data tables on my website. Despite trying various methods, I have not been able to achieve success. Currently, I am using the smart admin latest theme for development. When I make a call to the API, ...

Looking to compare the values of objects in one array with the values of objects in another array

Within this code snippet, I am attempting to retrieve the id of a variant that aligns with the selected objects const selected = [ { "id": 14 }, { "id": 95 } ] const variants = [ { "id": 1, "option_values": ...

Combining sub-array into main property

After organizing my data, I am looking to convert it into a flat structure data = [ { "Name":"Gorba Technology", "Product":"Dell", "City":[ "Delhi", "Mu ...

What is the best way to combine two arrays by sorting them according to their respective years?

I have two separate arrays: one containing values by year and order ID, and the other with different data. My goal is to combine them into a single array based on the year. let array1 = [{orderId: 1, year: 2020, value: 15}, {orderId: 1, yea ...

Updating files in a Next.js project and automatically refreshing the page without the need to rerun 'npm run'

For a while now, I've been developing websites using a traditional LAMP stack with javascript (including jQuery) for the frontend. Recently, I decided to explore using javascript for the backend as well and started learning next.js. In the older meth ...

Retain the parameter name when defining a function type mapping

Imagine you need to change the result of a function: (bob: Bob) => R => (bob: Bob) => R2 Is it possible to achieve this without changing the argument name? (e.g bob instead of using a general name like a) ...

The functionality of CSS transitions may be affected when dynamically adding a class

Creating a custom CSS for my main div: .main { height: 0%; position: absolute; width: 100%; z-index: 100; background: whitesmoke; bottom: 0; border-radius: 15px; padding: 20px; color: gray; left: 0; right: 0; transition: height 1s e ...

Navigating through various product categories in Angular's routing system

Greetings! I am currently building a Shop Page in Angular 4 and encountering an obstacle with Angular routing. The issue arises when a user clicks on a product category, the intention is for the website to direct them to the shop page. On the homepage, th ...

Obtaining attributes of a class from an object passed into the constructor

Consider the following code snippet: interface MyInterface { readonly valA: number; readonly valB: number; } class MyClass { readonly valA: number; readonly valB: number; constructor(props: MyInterface) { this.valA = props.val ...

Is it possible to leverage ES6 modules within a Node.js application while also debugging it using Visual Studio?

Trying to create a basic node.js module test project using ES6 in Visual Studio 2015 has resulted in build errors, preventing me from running or debugging the application. Could it be that I arrived at the party too soon? I attempted opening and building ...

I have the capability to successfully input data into Azure table storage, however, I am currently facing difficulties when attempting to retrieve information from the table

Currently, I am using Node.js with TS Express server. My goal is to retrieve data from a table named tracker that also has the partition name as tracker. While I have successfully been able to write to the table without any issues, I encountered an error w ...