timlrx / contentlayer2

Contentlayer turns your content into data - making it super easy to import MD(X) and CMS content in your app
https://contentlayer.dev
MIT License
192 stars 12 forks source link

TypeError: React.Component is not a constructor #40

Open niklaswallerstedt opened 1 month ago

niklaswallerstedt commented 1 month ago

Hey @timlrx. First off, thanks a lot for maintaining this fork. After upgrading past 0.5.0 I'm starting to get these errors

⨯ TypeError: React.Component is not a constructor
    at eval (webpack-internal:///(rsc)/../../node_modules/.pnpm/react-dom@18.2.0_react@18.2.0/node_modules/react-dom/cjs/react-dom.development.js:13953:23)
    at eval (webpack-internal:///(rsc)/../../node_modules/.pnpm/react-dom@18.2.0_react@18.2.0/node_modules/react-dom/cjs/react-dom.development.js:29862:5)
    at (rsc)/../../node_modules/.pnpm/react-dom@18.2.0_react@18.2.0/node_modules/react-dom/cjs/react-dom.development.js ([project_root]/packages/web/.next/server/vendor-chunks/react-dom@18.2.0_react@18.2.0.js:20:1)
    at __webpack_require__ ([project_root]/packages/web/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///(rsc)/../../node_modules/.pnpm/react-dom@18.2.0_react@18.2.0/node_modules/react-dom/index.js:32:20)
    at (rsc)/../../node_modules/.pnpm/react-dom@18.2.0_react@18.2.0/node_modules/react-dom/index.js ([project_root]/packages/web/.next/server/vendor-chunks/react-dom@18.2.0_react@18.2.0.js:30:1)
    at __webpack_require__ ([project_root]/packages/web/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///(rsc)/../../node_modules/.pnpm/mdx-bundler@10.0.2_esbuild@0.21.5/node_modules/mdx-bundler/dist/client.js:10:40)
    at (rsc)/../../node_modules/.pnpm/mdx-bundler@10.0.2_esbuild@0.21.5/node_modules/mdx-bundler/dist/client.js ([project_root]/packages/web/.next/server/vendor-chunks/mdx-bundler@10.0.2_esbuild@0.21.5.js:30:1)
    at __webpack_require__ ([project_root]/packages/web/.next/server/webpack-runtime.js:33:42) {
  type: 'TypeError',
  page: '/example-blog-post'
}

I've narrowed it down to my monorepo resolving react/react-dom to 18.2.0. If I override the resolved version it works again. While it may not be something on your end to fix, just reporting it in case there is something that can be fixed.

  "resolutions": {
    "react": "18.3.1",
    "react-dom": "18.3.1"
  }

I pulled out bits and pieces from another repo in a reproduction.

Node: v18.20.4

niklaswallerstedt commented 1 month ago

Another solution is to use next-mdx-remote instead of this https://contentlayer.dev/docs/sources/files/mdx-d747e46d#app-directory

import { MDXRemote } from 'next-mdx-remote/rsc';

...

<MDXRemote source={post.body.raw} components={customComponents} />
timlrx commented 1 month ago

Hi @niklaswallerstedt, thanks for the example repo. I tried it out and notice that it works if installed with npm or yarn, but I am not sure what's the issue with the resolution that results in the error for pnpm.

niklaswallerstedt commented 1 month ago

I haven't tested with npm or yarn yet but I'll give it a go.

niklaswallerstedt commented 1 month ago

Just to clarify this started from v0.5.0 and if you downgrade contentlayer2 it should still work. At least in my project going to verify in the repro as well