vercel / next.js

The React Framework
https://nextjs.org
MIT License
125.92k stars 26.87k forks source link

Importing Next.js Link component in Route Handler results in "Could not parse module" #69485

Closed fuma-nama closed 1 month ago

fuma-nama commented 1 month ago

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/7ystyy

To Reproduce

  1. Start the app in dev mode
  2. Go to http://localhost:3000/api

Current vs. Expected behavior

It throws an error:

Error: Could not parse module '[project]/node_modules/.pnpm/next@14.2.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/future/route-modules/app-route/vendored/contexts/router-context.js'
    at Object.<anonymous> (/project/workspace/.next/server/chunks/node_modules__pnpm_ae6303._.js:4561:11)
    at [project]/node_modules/.pnpm/next@14.2.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/future/route-modules/app-route/vendored/contexts/router-context.js [app-route] (ecmascript) (/project/workspace/.next/server/chunks/node_modules__pnpm_ae6303._.js:4564:3)
    at instantiateModule (/project/workspace/.next/server/chunks/[turbopack]_runtime.js:520:23)
    at getOrInstantiateModuleFromParent (/project/workspace/.next/server/chunks/[turbopack]_runtime.js:572:12)
    at commonJsRequire (/project/workspace/.next/server/chunks/[turbopack]_runtime.js:136:20)
    at Object.<anonymous> (/project/workspace/.next/server/chunks/node_modules__pnpm_ae6303._.js:4928:37)
    at [project]/node_modules/.pnpm/next@14.2.7_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/client/link.js [app-route] (ecmascript) (/project/workspace/.next/server/chunks/node_modules__pnpm_ae6303._.js:5346:3)
    at instantiateModule (/project/workspace/.next/server/chunks/[turbopack]_runtime.js:520:23)
    at getOrInstantiateModuleFromParent (/project/workspace/.next/server/chunks/[turbopack]_runtime.js:572:12)
    at commonJsRequire (/project/workspace/.next/server/chunks/[turbopack]_runtime.js:136:20)
    at Object.<anonymous> (/project/workspace/.next/server/chunks/node_modules__pnpm_ae6303._.js:5349:18)
    at [project]/node_modules/.pnpm/next@14.2.7_react-dom@18.2.0_react@18.2.0/node_modules/next/link.js [app-route] (ecmascript) (/project/workspace/.next/server/chunks/node_modules__pnpm_ae6303._.js:5351:3)
    ...

(see the rest of the error in CodeSandbox)

Ideally, it should ignore Next.js itself, or be able to parse/bundle it correctly.

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
  Available memory (MB): 4102
  Available CPU cores: 2
Binaries:
  Node: 20.9.0
  npm: 9.8.1
  Yarn: 1.22.19
  pnpm: 8.10.2
Relevant Packages:
  next: 14.2.7 // Latest available version is detected (14.2.7).
  eslint-config-next: 14.1.0
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.3
Next.js Config:
  output: N/A

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

Turbopack

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

next dev (local)

Additional context

I agree that it's probably weird to import Next.js components (Image/Link) from route handlers. For my case, it's because I use a MDX loader, and the transformed MDX file exports necessary frontmatter information, which is needed to construct the response.

I have to import the MDX file from a route handler:

import { frontmatter } from "./file.mdx"

export function GET() {
  // get frontmatter info and return a response
}

Because the file.mdx file has imports to Next.js Link component, this causes the problem above.

Pagebakers commented 1 month ago

Same cryptic error is happening when using Next Auth with turbo enabled.

https://github.com/nextauthjs/next-auth/issues/11674

Not even sure where this is referenced, but that path/file does not even exist: node_modules/next/dist/server/future/route-modules/app-route/vendored/contexts/router-context.js

timneutkens commented 1 month ago

Just verified this is already fixed on next@canary, please make sure to check next@canary as there's hundreds of changes to Turbopack on canary currently 👍

github-actions[bot] commented 3 weeks ago

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.