vercel/next.js

MDX Plugin String Format resolution doesn't work with ESM plugins with multiple exports

Opened this issue · 1 comments

Link to the code that reproduces this issue

https://github.com/wesbos/next-mdx-plugin-string-issue

To Reproduce

When using strings to import a MDX Rehype plugin, the Next.js importer fails.

I believe this is only when the package has multiple ESM exports - like this package: https://github.com/stefanprobst/rehype-extract-toc/blob/main/package.json

Thansk to @karlhorky for linking me to the possible commit / code. CC @timneutkens

https://github.com/vercel/next.js/pull/72802/files#diff-d5904dff78d88856dc003d263e6f70f0a607166230fba8e4b947a3bae5e4e87cR10

import createMDX from "@next/mdx";

const withMDX = createMDX({
  options: {
    rehypePlugins: [
      ["@stefanprobst/rehype-extract-toc"],
      ["@stefanprobst/rehype-extract-toc/mdx"],
    ],
  },
});

We get the Error:

Error: No "exports" main defined in /Users/wesbos/Sites/delete-me/mdx-plugin-issue/node_modules/@stefanprobst/rehype-extract-toc/package.json
    at Array.map (<anonymous>) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}
 ⨯ unhandledRejection: Error: No "exports" main defined in /Users/wesbos/Sites/delete-me/mdx-plugin-issue/node_modules/@stefanprobst/rehype-extract-toc/package.json
    at Array.map (<anonymous>) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}
 ⨯ unhandledRejection:  Error: No "exports" main defined in /Users/wesbos/Sites/delete-me/mdx-plugin-issue/node_modules/@stefanprobst/rehype-extract-toc/package.json
    at Array.map (<anonymous>) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Or

Error: Package subpath './mdx' is not defined by "exports" in /Users/wesbos/Sites/delete-me/mdx-plugin-issue/node_modules/@stefanprobst/rehype-extract-toc/package.json
    at Array.map (<anonymous>) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

This error does not happen if the plugin is imported inside next.config.mjs and passed as a javascript function, but since Turborepo must pass as a string, I cannot do this.

The error exists both with and without --turbo.

Current vs. Expected behavior

_

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.1.0: Thu Oct 10 21:03:15 PDT 2024; root:xnu-11215.41.3~2/RELEASE_ARM64_T6000
  Available memory (MB): 65536
  Available CPU cores: 10
Binaries:
  Node: 23.1.0
  npm: 10.9.0
  Yarn: 1.22.22
  pnpm: 9.10.0
Relevant Packages:
  next: 15.0.4-canary.51 // Latest available version is detected (15.0.4-canary.51).
  eslint-config-next: N/A
  react: 19.0.0-beta-04b058868c-20240508
  react-dom: 19.0.0-beta-04b058868c-20240508
  typescript: 5.1.3
Next.js Config:
  output: N/A

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

Markdown (MDX)

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

next dev (local)

Additional context

No response

wesbos commented

It looks like the issue here is when the package doesn't have a "default" export - see the above PR that fixed it for me. I still think this is a bug though because I could import the package without issue, it only happened when passing a string