nextjs - ✅(Solved) Fix Turbopack: notFound() in Route Handler causes build crash [1 pull requests, 4 comments, 4 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#86390Fetched 2026-04-08 02:11:13
View on GitHub
Comments
4
Participants
4
Timeline
20
Reactions
3
Author
Timeline (top)
referenced ×9commented ×4labeled ×4cross-referenced ×2

Error Message

$ pnpm build

@ build /project/workspace next build

Attention: Next.js now collects completely anonymous telemetry regarding usage. This information is used to shape Next.js' roadmap and prioritize features. You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL: https://nextjs.org/telemetry

▲ Next.js 16.0.2-canary.27 (Turbopack)

Creating an optimized production build ... ✓ Compiled successfully in 2.7s ✓ Finished TypeScript in 2.3s
Collecting page data using 3 workers ..Error: Could not parse module '[project]/node_modules/.pnpm/next@[email protected]_react@19.2.0/node_modules/next/dist/server/route-modules/app-route/vendored/contexts/app-router-context.js', file not found at instantiateModule (.next/server/chunks/[turbopack]_runtime.js:715:9) at getOrInstantiateModuleFromParent (.next/server/chunks/[turbopack]_runtime.js:738:12) at Context.commonJsRequire [as r] (.next/server/chunks/[turbopack]_runtime.js:249:12) at instantiateModule (.next/server/chunks/[turbopack]_runtime.js:715:9) at getOrInstantiateModuleFromParent (.next/server/chunks/[turbopack]_runtime.js:738:12) at Context.commonJsRequire [as r] (.next/server/chunks/[turbopack]_runtime.js:249:12) at instantiateModule (.next/server/chunks/[turbopack]_runtime.js:715:9) { code: 'MODULE_UNPARSABLE' }

Build error occurred Error: Failed to collect page data for /a at ignore-listed frames { type: 'Error' }  ELIFECYCLE  Command failed with exit code 1.

Fix Action

Fixed

PR fix notes

PR #10: Fix Turbopack crash with Route Handlers using notFound()

Description (problem / solution / changelog)

What?

  • Fixes a build crash in Turbopack when Route Handlers call the notFound() function.
  • Adds a try-catch mechanism to handle the absence of app-router-context during Turbopack builds, exporting an empty namespace instead.
  • Ensures Route Handlers do not require app-router-context for their operations with Turbopack.

Why?

  • The build currently crashes due to shared-modules.ts attempting to import a module (app-router-context) that is not needed nor available in this context.
  • Resolves issue #86390.

How?

  • Introduced a try-catch block to provide fallback behavior during module import.
  • Added a test to validate successful building of Route Handlers using notFound() with Turbopack.

Closes NEXT- Fixes #86390

Changed files

  • packages/next/src/server/route-modules/app-route/shared-modules.ts (modified, +19/-1)
  • test/production/app-dir/turbopack-route-handler-notfound/app/api/not-found/route.ts (added, +5/-0)
  • test/production/app-dir/turbopack-route-handler-notfound/app/layout.tsx (added, +13/-0)
  • test/production/app-dir/turbopack-route-handler-notfound/app/page.tsx (added, +5/-0)
  • test/production/app-dir/turbopack-route-handler-notfound/index.test.ts (added, +33/-0)

Code Example

$ pnpm build

> @ build /project/workspace
> next build

Attention: Next.js now collects completely anonymous telemetry regarding usage.
This information is used to shape Next.js' roadmap and prioritize features.
You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
https://nextjs.org/telemetry

Next.js 16.0.2-canary.27 (Turbopack)

  Creating an optimized production build ...
Compiled successfully in 2.7s
Finished TypeScript in 2.3s    
  Collecting page data using 3 workers  ..Error: Could not parse module '[project]/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/server/route-modules/app-route/vendored/contexts/app-router-context.js', file not found
    at instantiateModule (.next/server/chunks/[turbopack]_runtime.js:715:9)
    at getOrInstantiateModuleFromParent (.next/server/chunks/[turbopack]_runtime.js:738:12)
    at Context.commonJsRequire [as r] (.next/server/chunks/[turbopack]_runtime.js:249:12)
    at instantiateModule (.next/server/chunks/[turbopack]_runtime.js:715:9)
    at getOrInstantiateModuleFromParent (.next/server/chunks/[turbopack]_runtime.js:738:12)
    at Context.commonJsRequire [as r] (.next/server/chunks/[turbopack]_runtime.js:249:12)
    at instantiateModule (.next/server/chunks/[turbopack]_runtime.js:715:9) {
  code: 'MODULE_UNPARSABLE'
}

> Build error occurred
Error: Failed to collect page data for /a
    at ignore-listed frames {
  type: 'Error'
}
ELIFECYCLECommand failed with exit code 1.

---

$ pnpm build --webpack

> @ build /project/workspace
> next build "--webpack"

Next.js 16.0.2-canary.27 (webpack)

  Creating an optimized production build ...
Compiled successfully in 5.2s
Finished TypeScript in 2.2s    
Collecting page data using 3 workers in 372.6ms    
Generating static pages using 3 workers (5/5) in 182.6ms
Collecting build traces in 8.7s    
Finalizing page optimization in 8.7s    

Route (app)
┌ ○ /
├ ○ /_not-found
└ ƒ /a


  (Static)   prerendered as static content
ƒ  (Dynamic)  server-rendered on demand

---

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
  Available memory (MB): 8198
  Available CPU cores: 4
Binaries:
  Node: 20.12.1
  npm: 10.5.0
  Yarn: 1.22.19
  pnpm: 8.15.6
Relevant Packages:
  next: 16.0.2-canary.27 // Latest available version is detected (16.0.2-canary.27).
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: 5.9.3
Next.js Config:
  output: N/A
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/stoic-ramanujan-c7kl2j?file=%2Fapp%2Fa%2Froute.ts%3A1%2C1-6%2C1

<img width="1434" height="852" alt="Image" src="https://github.com/user-attachments/assets/c95d4fa9-fa0d-4a1a-87ca-fed8270d5f69" />

To Reproduce

  1. Add a Route Handler with an exported GET function
  2. Use notFound() from next/navigation in the Route Handler GET function body
  3. Run build with Turbopack -> fails with confusing error message 💥
  4. Run build with webpack -> succeeds (Route Handler returns 404)

Current vs. Expected behavior

Turbopack returns error with messages:

  1. Could not parse module (MODULE_UNPARSABLE)
  2. file not found (related to the file [project]/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/server/route-modules/app-route/vendored/contexts/app-router-context.js)
  3. Failed to collect page data
$ pnpm build

> @ build /project/workspace
> next build

Attention: Next.js now collects completely anonymous telemetry regarding usage.
This information is used to shape Next.js' roadmap and prioritize features.
You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
https://nextjs.org/telemetry

▲ Next.js 16.0.2-canary.27 (Turbopack)

  Creating an optimized production build ...
✓ Compiled successfully in 2.7s
✓ Finished TypeScript in 2.3s    
  Collecting page data using 3 workers  ..Error: Could not parse module '[project]/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/server/route-modules/app-route/vendored/contexts/app-router-context.js', file not found
    at instantiateModule (.next/server/chunks/[turbopack]_runtime.js:715:9)
    at getOrInstantiateModuleFromParent (.next/server/chunks/[turbopack]_runtime.js:738:12)
    at Context.commonJsRequire [as r] (.next/server/chunks/[turbopack]_runtime.js:249:12)
    at instantiateModule (.next/server/chunks/[turbopack]_runtime.js:715:9)
    at getOrInstantiateModuleFromParent (.next/server/chunks/[turbopack]_runtime.js:738:12)
    at Context.commonJsRequire [as r] (.next/server/chunks/[turbopack]_runtime.js:249:12)
    at instantiateModule (.next/server/chunks/[turbopack]_runtime.js:715:9) {
  code: 'MODULE_UNPARSABLE'
}

> Build error occurred
Error: Failed to collect page data for /a
    at ignore-listed frames {
  type: 'Error'
}
 ELIFECYCLE  Command failed with exit code 1.

webpack builds and serves 404 from the Route Handler:

$ pnpm build --webpack

> @ build /project/workspace
> next build "--webpack"

▲ Next.js 16.0.2-canary.27 (webpack)

  Creating an optimized production build ...
✓ Compiled successfully in 5.2s
✓ Finished TypeScript in 2.2s    
✓ Collecting page data using 3 workers in 372.6ms    
✓ Generating static pages using 3 workers (5/5) in 182.6ms
✓ Collecting build traces in 8.7s    
✓ Finalizing page optimization in 8.7s    

Route (app)
┌ ○ /
├ ○ /_not-found
└ ƒ /a


(Static)   prerendered as static content
ƒ  (Dynamic)  server-rendered on demand
<img width="1109" height="508" alt="Image" src="https://github.com/user-attachments/assets/6ac1b569-8492-4452-9ddc-7e3fd4710a95" />

Expectation:

Either:

  1. Turbopack works the same way as webpack
  2. Turbopack fails with an error message that makes it clear

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): 8198
  Available CPU cores: 4
Binaries:
  Node: 20.12.1
  npm: 10.5.0
  Yarn: 1.22.19
  pnpm: 8.15.6
Relevant Packages:
  next: 16.0.2-canary.27 // Latest available version is detected (16.0.2-canary.27).
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: 5.9.3
Next.js Config:
  output: N/A

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

Not Found, Route Handlers

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

next build (local)

Additional context

--

extent analysis

TL;DR

The issue can be resolved by using a try-catch block to handle the notFound() function from next/navigation in the Route Handler GET function body, allowing Turbopack to build without errors.

Guidance

  • Verify that the notFound() function is being used correctly in the Route Handler GET function body.
  • Try using a try-catch block to handle any potential errors thrown by the notFound() function.
  • Check the Next.js documentation for any known issues or limitations with using notFound() with Turbopack.
  • Compare the build configurations for Turbopack and webpack to identify any differences that may be contributing to the issue.

Example

import { notFound } from 'next/navigation';

export async function GET() {
  try {
    // code that may throw an error
    return notFound();
  } catch (error) {
    // handle the error
    console.error(error);
    return new Response('Error occurred', { status: 500 });
  }
}

Notes

The issue appears to be specific to Turbopack and may be related to how it handles errors thrown by the notFound() function. The try-catch block example provided is a potential workaround, but the root cause of the issue may require further investigation.

Recommendation

Apply workaround: Using a try-catch block to handle the notFound() function can allow Turbopack to build without errors, providing a temporary solution until the root cause of the issue is identified and addressed.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING