next.js: As of [15.2.0-canary.57] throws TypeError: Cannot read properties of undefined (reading 'stack')

Link to the code that reproduces this issue

https://github.com/talatkuyuk/next15-canary-bug-reproduction

To Reproduce

The app which I provided its link is a bug reproduction for next@15.2.0-canary.57 and still continues in next@15.2.0-canary.70 which is latest canary.

It throws the error below in only development mode, not in production.

The error occurs in the nextjs page (MDX article) related with importing a .mjs react component into MDX. I put another article page to show importing a module/function in MDX works well without problem.

TypeError: Cannot read properties of undefined (reading 'stack')
    at stringify (<anonymous>)
    at stringify (<anonymous>) {
  digest: '3183793578'
}

Actually, there is no problem with the Next.js versions below (All, I tested). Importing a .mjs react component into MDX works well.

  • next@15.1.7
  • next@15.2.0-canary.0
  • next@15.2.0-canary.10
  • next@15.2.0-canary.20
  • next@15.2.0-canary.30
  • next@15.2.0-canary.40
  • next@15.2.0-canary.50
  • next@15.2.0-canary.56

Step to produce the error

npm run dev–> open browser and click to articles, and see the error in development mode.

npm run build and npm run start --> open browser and click the articles, see there is NO error in production mode.

Then, install the previous canary version to test it.

npm i -S next@v15.2.0-canary.56
rm -rf .next && npm run dev

and see there is NO error in development and production modes.

Then, install the canary.57 again,

npm i -S next@v15.2.0-canary.57
rm -rf .next && npm run dev

and see the TypeError in development mode.

What is next-mdx-remote-client

The next-mdx-remote-client is a fork of next-mdx-remote, having more features like supporting import statements in MDX.

It works well with all versions of Next.js (13, 14, 15) for one year almost.

Log in the console related with the error

I put it in the end because it is long:

[Error] TypeError: Cannot read properties of undefined (reading 'stack')
resolveErrorDev
processFullStringRow
processFullBinaryRow
progress

The above error occurred in the <Unknown> component. It was handled by the <DevOverlayErrorBoundary> error boundary.
	onCaughtError (error-boundary-callbacks.js:53)
	logCaughtError (react-dom-client.development.js:8138)
	runWithFiberInDEV (react-dom-client.development.js:1474)
	(anonim işlev) (react-dom-client.development.js:8185)
	callCallback (react-dom-client.development.js:4841)
	commitCallbacks (react-dom-client.development.js:4861)
	runWithFiberInDEV (react-dom-client.development.js:1474)
	commitClassCallbacks (react-dom-client.development.js:11062)
	commitLayoutEffectOnFiber (react-dom-client.development.js:12182)
	recursivelyTraverseLayoutEffects (react-dom-client.development.js:13296)
	commitLayoutEffectOnFiber (react-dom-client.development.js:12105)
	recursivelyTraverseLayoutEffects (react-dom-client.development.js:13296)
	commitLayoutEffectOnFiber (react-dom-client.development.js:12105)
	recursivelyTraverseLayoutEffects (react-dom-client.development.js:13296)
	commitLayoutEffectOnFiber (react-dom-client.development.js:12291)
	recursivelyTraverseLayoutEffects (react-dom-client.development.js:13296)
	commitLayoutEffectOnFiber (react-dom-client.development.js:12291)
	recursivelyTraverseLayoutEffects (react-dom-client.development.js:13296)
	commitLayoutEffectOnFiber (react-dom-client.development.js:12291)
	recursivelyTraverseLayoutEffects (react-dom-client.development.js:13296)
	commitLayoutEffectOnFiber (react-dom-client.development.js:12291)
	recursivelyTraverseLayoutEffects (react-dom-client.development.js:13296)
	commitLayoutEffectOnFiber (react-dom-client.development.js:12291)
	recursivelyTraverseLayoutEffects (react-dom-client.development.js:13296)
	commitLayoutEffectOnFiber (react-dom-client.development.js:12291)
	recursivelyTraverseLayoutEffects (react-dom-client.development.js:13296)
	commitLayoutEffectOnFiber (react-dom-client.development.js:12105)
	recursivelyTraverseLayoutEffects (react-dom-client.development.js:13296)
	commitLayoutEffectOnFiber (react-dom-client.development.js:12110)
	recursivelyTraverseLayoutEffects (react-dom-client.development.js:13296)
	commitLayoutEffectOnFiber (react-dom-client.development.js:12105)
	recursivelyTraverseLayoutEffects (react-dom-client.development.js:13296)
	commitLayoutEffectOnFiber (react-dom-client.development.js:12105)
	recursivelyTraverseLayoutEffects (react-dom-client.development.js:13296)
	commitLayoutEffectOnFiber (react-dom-client.development.js:12291)
	recursivelyTraverseLayoutEffects (react-dom-client.development.js:13296)
	commitLayoutEffectOnFiber (react-dom-client.development.js:12105)
	recursivelyTraverseLayoutEffects (react-dom-client.development.js:13296)
	commitLayoutEffectOnFiber (react-dom-client.development.js:12105)
	recursivelyTraverseLayoutEffects (react-dom-client.development.js:13296)
	commitLayoutEffectOnFiber (react-dom-client.development.js:12291)
	recursivelyTraverseLayoutEffects (react-dom-client.development.js:13296)
	commitLayoutEffectOnFiber (react-dom-client.development.js:12291)
	recursivelyTraverseLayoutEffects (react-dom-client.development.js:13296)
	commitLayoutEffectOnFiber (react-dom-client.development.js:12187)
	flushLayoutEffects (react-dom-client.development.js:17269)
	commitRoot (react-dom-client.development.js:17106)
	commitRootWhenReady (react-dom-client.development.js:16183)
	performWorkOnRoot (react-dom-client.development.js:16087)
	performWorkOnRootViaSchedulerTask (react-dom-client.development.js:17995)
	performWorkUntilDeadline (scheduler.development.js:36)

Current vs. Expected behavior

Expecting no error, and show the article page without problem.

Provide environment information

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 24.3.0: Thu Jan  2 20:22:00 PST 2025; root:xnu-11215.81.4~3/RELEASE_X86_64
  Available memory (MB): 16384
  Available CPU cores: 12
Binaries:
  Node: 18.20.5
  npm: 10.8.2
  Yarn: 1.22.22
  pnpm: 10.4.1
Relevant Packages:
  next: 15.2.0-canary.57 // There is a newer canary version (15.2.0-canary.70) available, please upgrade! 
  eslint-config-next: 15.1.7
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.7.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Not sure

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

It throws the error in only development mode in local, not in production.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 36
  • Comments: 32 (10 by maintainers)

Commits related to this issue

Most upvoted comments

This workaround solved it for me:

const nextConfig = {
  transpilePackages: ["next-mdx-remote"],
};

Interesting conversation. For what it’s worth, I’m not using “next-mdx-remote-client” but "“next-mdx-remote”. Together with the upgrade to Next 15.2.0, I also upgraded “next-mdx-remote” to version 5.0.0. Since then I’m also getting in development mode these errors:

⨯ [TypeError: Cannot read properties of undefined (reading ‘stack’)] { digest: ‘890473477’

In production, these errors don’t occur.

Basically all is similar to the behaviour described above for the “next-mdx-remote-client”.

Same as the others, I’m also getting the same behavior described above with contentlayer2, on any Next versions above 15.2.0-canary.56.

Production builds run fine, only getting this error in dev.

Fixed in next-mdx-remote-client@2.1.0.

It worked for me in the following scenario:

  • next-mdx-remote-client@2.1.1
  • next@15.2.0

My code was previously using:

import { compileMDX } from "next-mdx-remote/rsc";

// ...

const { content } = await compileMDX({ /* ... */ });

I changed it to:

import { evaluate } from "next-mdx-remote-client/rsc";

// ...

const { content } = await evaluate({ /* ... */ });

This new package works

  • in dev mode with next dev --turbopack
  • in a local build with next build followed by next start
  • when deployed to Vercel

After this, next-mdx-remote can be removed altogether and next-mdx-remote-client is not needed in transpilePackages either.

Hi @eps1lon, thank you for reply, and for taking your time to solve the problem.

Are you saying that the problem is not caused by a change in 15.2.0-canary.57, but that there is an inherent situation in Reflect.construct regarding the import of the react within?

Even if such a situation is true, I’m trying to understand why the same problem did not occur in previous next versions. In all previous versions of next, there was no problem and the mdx compiled source is being able to be hydrated via Reflect.construct while mdx source is importing a .mjs react component. (in app router, not pages router)

  • next@13 --> there is no problem
  • next@14 --> there is no problem
  • next@14.2.24 (latest next@14) --> there is no problem
  • next@15.1 --> there is no problem
  • next@15.1.7 --> there is no problem
  • next@15.2.0-canary.10 --> there is no problem
  • next@15.2.0-canary.20 --> there is no problem
  • next@15.2.0-canary.30 --> there is no problem
  • next@15.2.0-canary.40 --> there is no problem
  • next@15.2.0-canary.50 --> there is no problem
  • next@15.2.0-canary.56 --> there is no problem
  • next@15.2.0-canary.57 --> The issue has started to appear.
  • next@15.2.0 --> The issue still occurs

If your diagnosis was correct, why does the issue NOT occur in production and the error occurs only in development environment?

Evaluation and hydration of compiled source in such a case in next-mdx-remote-client/rsc occurs in server side. It doesn’t use dynamic import import(). And, the importing the module/component does not occur in the client side in MDX evaluation, but in rsc environment.

In Next.js, the “Dev Overlay” is an interactive error overlay that helps developers identify and debug issues while running the app in development mode (next dev). I think the issue may be related with dev overlay. Because I see some commits about dev overlay in next@15.2.0-canary.57. For example, the dev overlay is enabled by default. I see also a commit about [dev overlay] fix: call stack ... See all commits (changes) in that version. (it is so similar with TypeError: Cannot read properties of undefined (reading ‘stack’))

I need to get the issue solved. May other expertises also see the issue? @ztanner @devjiwonchoi

Fixed in next-mdx-remote-client@2.1.0.

The affected library need to pass in the React module into the evaluated code instead of having the evaluated code import React which wouldn’t be affected by bundlers.

I’ve passed the React module into the evaluated code in the new version v2.1.0 of next-mdx-remote-client . I made the React is available in the imported components.

Now, next-mdx-remote-client works with next@15.2.2 without problem.

You can have a look for more explanation here https://github.com/ipikuka/next-mdx-remote-client/issues/9#issuecomment-2707458632.

I don’t know how other libraries suffers from the issue, it may provide an inspiration for further solution.

[ update 11/03 ]

Next.js v15.2.2 has been tested, and next-mdx-remote-client still works with no problem since the evaluated code has a React instance in which is passed internally.

In my case it was next-seo too thank you @talatkuyuk

@jpmonette that worked for me too. I’m using next-seo where I had a problem with the <BreadcrumbJsonLd useAppDir itemListElements={jsonLdBreadcrumbs} /> component. Using this in dev would break the app, but in prod it works fine. The transpilePackages fix will do for now.

Is there an update on this?

This is a pre-existing bug with these libraries. E.g. React.useId() wasn’t working either. The affected library need to pass in the React module into the evaluated code instead of having the evaluated code import React which wouldn’t be affected by bundlers.

Hi @AntoineKM,

next-mdx-remote-client works without any issues including this one. if you can provide a minimal reproduction, I can solve the issue more efficiently.

If the issue is related to the onruntime-website, I noticed that it also uses next-seo, which is affected by this issue. See the comment.

I switched from next-mdx-remote to next-mdx-remote-client, and i still have the same issue 👀

Hi @talatkuyuk,

Thank you for your insights.

Regarding cloneElement, I was limited by its compatibility with the new Lynx framework.

Additionally, return React.cloneElement(element, { value }) makes value accessible as element.props.value rather than element.value, which is what I intended


I agree that this is not understandable

There may be multiple reasons causing this error. However, for those who want to reproduce the issue:

// src/app/page.tsx
const MyComponent = () => {
  return <div>Test</div>;
};

const myElement = <MyComponent />;

const Page = () => {
  return <>{{ ...myElement }}</>; // TypeError: Cannot read properties of undefined (reading 'stack') at stringify (<anonymous>) { digest: '274730797'
};

// With async page ↓

// const Page = async () => {
//   return <>{{ ...myElement }}</>; //  [cause]: [TypeError: Cannot read properties of undefined (reading 'stack')] { digest: '1675862269' }
// };

export default Page;

I remember when Next.js upgraded from 14 to 15, I had a similar issue with Contentlayer, so I switched to next-mdx-remote. Now it’s happening again. Maybe we should move to a headless CMS or an API-based solution.

@talatkuyuk, thanks for the insightful feedback.

My solution works within a fairly specific set of constraints that might not be applicable to everyone. Specifically, I’m taking advantage of Next.js’s App Router and Server Components architecture. This allows me to perform the entire MDX evaluation on the server, which inherently avoids the client-side React versioning and bundling issues that seem to be at the root of the reported problems. The use of ‘baseUrl’ also becomes unnecessary as everything is being rendered on the server side without further processing on the client side.

Furthermore, I’m carefully managing the complexity of my MDX content. I am not using dynamic import statements within the MDX. I mostly rely on passing pre-imported, server-side React components to the <MDXContent> component via the components prop, and not through imports within the MDX content. The use of useId() within MDX-components also doesn’t occur.

Therefore, it seems that my scenario effectively operates within a “clean room,” avoiding the conditions that trigger the React version conflicts. While my current approach works for my use case, I recognize that it’s not a general solution for all MDX implementations.

Hi @activemonkeys,

Your solution doesn’t touch the issue. Passing React into evaluate of @mdx-js/mdx is affectless, because the evaluate accepts explicitly and only {Fragment, baseUrl, jsx, jsxDEV, jsxs, useMDXComponents} as runtime options. check here

And, typescript throws an error Object literal may only specify known properties, and 'React' does not exist in type 'Readonly<EvaluateOptions>'.ts(2353) and warning us appropriately. You can disregard the error, and pass React into evaluate explicitly, but this doesn’t change anything.

Meaningly, the React which passed into evaluate of @mdx-js/mdx is doing nothing.

Why do you see that CustomMDX works ?

Because, you didn’t test it against a MDX source which contains an import declaration for a React component.

Add baseUrl option into evaluate (otherwise imports don’t work)

  // ...
  const { default: MDXContent } = await evaluate(source, {
    ...runtime,
    React,  // I already mentioned it is affectless !
+    baseUrl: import.meta.url,
  });
  // ...

page.tsx

// ...
export default function Home() {
  const source = "# Import\n\nimport Test from './Test.mjs';\n\n<Test />";

  return (<CustomMDX source={source} />);
}

Test.mjs(place it alongside with CustomMDX.tsx)

import * as React from "react";

export default function Test() {
  React.useId();
  return "Hello, Dave!";
}

And you will see the same error:

TypeError: Cannot read properties of null (reading 'useId')
    at Test (components/Test.mjs:4:9) {
  digest: '604687449'
}

Thanks @talatkuyuk, within my own project I solved it without an external package. This is the code I’m using that works with Next.js 15.2.0: A React component that safely renders MDX content in Next.js applications by explicitly passing the React runtime to avoid version conflicts that cause errors in Next.js 15.2.0+. Maybe this will be helpful to you…

/**
 * CustomMDX Component
 * 
 * This component renders MDX content in a Next.js application using the @mdx-js/mdx library.
 * It avoids the common issue found in Next.js 15.2.0+ where MDX evaluation causes React version conflicts
 * by explicitly passing the React runtime to the MDX evaluator.
 */

import * as React from 'react';
import * as runtime from 'react/jsx-runtime';
import {evaluate} from '@mdx-js/mdx';
import {components} from './components';

/**
 * Props for the CustomMDX component
 * 
 * @property {string} source - The MDX content as a string to be rendered
 * @property {Record<string, React.ComponentType<any>>} [additionalComponents] - Optional additional components 
 * to be made available to the MDX content
 */
interface CustomMDXProps {
  source: string;
  additionalComponents?: Record<string, React.ComponentType<any>>;
}

/**
 * Renders MDX content with provided components
 * 
 * This component evaluates MDX source content and renders it with the provided components.
 * It passes the React runtime directly to the MDX evaluator to avoid React version conflicts,
 * which is a common issue in Next.js 15.2.0+ with MDX libraries that use dynamic evaluation.
 * 
 * @param {CustomMDXProps} props - The component props
 * @returns {JSX.Element} The rendered MDX content or an error message if rendering fails
 */
export default async function CustomMDX({
  source,
  additionalComponents,
}: CustomMDXProps) {
  try {
    // Evaluate the MDX content with the React runtime
    // This approach prevents the "Cannot read properties of undefined (reading 'stack')" error
    // by explicitly providing the React runtime instead of letting MDX import it dynamically
    const {default: MDXContent} = await evaluate(source, {
      ...runtime,
      // Explicitly pass React to ensure consistent versioning
      React,
    });
    
    // Merge default components with any additional components provided
    const mergedComponents = {
      ...components,
      ...(additionalComponents || {}),
    };
    
    return <MDXContent components={mergedComponents} />;
  } catch (error) {
    console.error('Error rendering MDX:', error);
    return (
      <div>An error occurred while rendering the content.</div>
    );
  }
}