Skip to content

MDX in app router causes - "error createContext only works in Client Components" #50110

@MatthewCaseres

Description

@MatthewCaseres

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.1.0: Sun Oct  9 20:15:09 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T6000
    Binaries:
      Node: 19.6.0
      npm: 9.4.0
      Yarn: N/A
      pnpm: 8.4.0
    Relevant packages:
      next: 13.4.4-canary.0
      eslint-config-next: 13.4.3
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.0.4

Which example does this report relate to?

https://nextjs.org/docs/app/building-your-application/configuring/mdx

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

When I follow the documentation for using MDX with app router, I get the following error -

- error node_modules/@mdx-js/react/lib/index.js (34:26) @ React
- error createContext only works in Client Components. Add the "use client" directive at the top of the file to use it. Read more: https://nextjs.org/docs/messages/context-in-server-component
    at eval (webpack-internal:///(sc_server)/./node_modules/@mdx-js/react/lib/index.js:38:73)
    at (sc_server)/./node_modules/@mdx-js/react/lib/index.js (/Users/matthewcaseres/Documents/GitHub/mdx-repro/.next/server/app/page.js:2273:1)
    at __webpack_require__ (/Users/matthewcaseres/Documents/GitHub/mdx-repro/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///(sc_server)/./src/app/hello.mdx:7:85)
    at (sc_server)/./src/app/hello.mdx (/Users/matthewcaseres/Documents/GitHub/mdx-repro/.next/server/app/page.js:286:1)
    at __webpack_require__ (/Users/matthewcaseres/Documents/GitHub/mdx-repro/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///(sc_server)/./src/app/page.tsx:7:68)
    at (sc_server)/./src/app/page.tsx (/Users/matthewcaseres/Documents/GitHub/mdx-repro/.next/server/app/page.js:2163:1)
    at Function.__webpack_require__ (/Users/matthewcaseres/Documents/GitHub/mdx-repro/.next/server/webpack-runtime.js:33:42)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  name: 'TypeError',
  digest: undefined
}
null
- error node_modules/@mdx-js/react/lib/index.js (34:26) @ React
- error createContext only works in Client Components. Add the "use client" directive at the top of the file to use it. Read more: https://nextjs.org/docs/messages/context-in-server-component
    at eval (webpack-internal:///(sc_server)/./node_modules/@mdx-js/react/lib/index.js:38:73)
    at (sc_server)/./node_modules/@mdx-js/react/lib/index.js (/Users/matthewcaseres/Documents/GitHub/mdx-repro/.next/server/app/page.js:2273:1)
    at __webpack_require__ (/Users/matthewcaseres/Documents/GitHub/mdx-repro/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///(sc_server)/./src/app/hello.mdx:7:85)
    at (sc_server)/./src/app/hello.mdx (/Users/matthewcaseres/Documents/GitHub/mdx-repro/.next/server/app/page.js:286:1)
    at __webpack_require__ (/Users/matthewcaseres/Documents/GitHub/mdx-repro/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///(sc_server)/./src/app/page.tsx:7:68)
    at (sc_server)/./src/app/page.tsx (/Users/matthewcaseres/Documents/GitHub/mdx-repro/.next/server/app/page.js:2163:1)
    at Function.__webpack_require__ (/Users/matthewcaseres/Documents/GitHub/mdx-repro/.next/server/webpack-runtime.js:33:42)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  name: 'TypeError',
  digest: undefined
}
null

Expected Behavior

I should be able to use MDX.

To Reproduce

https://github.com/MatthewCaseres/mdx-repro

Metadata

Metadata

Assignees

No one assigned

    Labels

    examplesIssue was opened via the examples template.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions