nextjs - 💡(How to fix) Fix [Turbopack] Duplicate class definitions across route chunks, breaking `instanceof` [2 comments, 3 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#89192Fetched 2026-04-08 02:03:09
View on GitHub
Comments
2
Participants
3
Timeline
6
Reactions
3
Timeline (top)
commented ×2cross-referenced ×1issue_type_added ×1labeled ×1

Error Message

grep -c "class e extends d.ServiceException" .next/server/app/api/*/route.js

Root Cause

Current: When multiple routes import the same class from an external package, Turbopack inlines a separate copy of the class definition into each route's bundle. This causes instanceof checks to fail across routes because each route has a different class object.

Fix Action

Fix / Workaround

As a workaround, I've added all of my AWS SDK clients to serverExternalPackages for now.

Code Example

grep -c "class e extends d.ServiceException" .next/server/app/api/*/route.js

---

.next/server/app/api/test/route.js:1
.next/server/app/api/test2/route.js:1

---

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.2.0
  Available memory (MB): 36864
  Available CPU cores: 12
Binaries:
  Node: 20.18.1
  npm: 10.8.2
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 15.5.10
  eslint-config-next: N/A
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.7.3
Next.js Config:
  output: N/A

---

if (error instanceof AccessDeniedException) { ... }
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/TrevorBurnham/nextjs-instanceof-api-error

To Reproduce

  1. Clone the repo and run npm install && npm run build
  2. Inspect the build output:
grep -c "class e extends d.ServiceException" .next/server/app/api/*/route.js

Output shows each route has its own class definition:

.next/server/app/api/test/route.js:1
.next/server/app/api/test2/route.js:1

Current vs. Expected behavior

Current: When multiple routes import the same class from an external package, Turbopack inlines a separate copy of the class definition into each route's bundle. This causes instanceof checks to fail across routes because each route has a different class object.

Expected: The class should be bundled once in a shared chunk and imported by all routes, preserving instanceof behavior.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.2.0
  Available memory (MB): 36864
  Available CPU cores: 12
Binaries:
  Node: 20.18.1
  npm: 10.8.2
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 15.5.10
  eslint-config-next: N/A
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.7.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 build (local), Other (Deployed), Vercel (Deployed)

Additional context

I noticed this when working with AWS SDK clients: I had some logic that checked

if (error instanceof AccessDeniedException) { ... }

where AccessDeniedException was imported from one of those client packages. In local dev, the condition was true as expected. But in production (with the Turbopack build), it'd return false!

As a workaround, I've added all of my AWS SDK clients to serverExternalPackages for now.

extent analysis

TL;DR

  • Consider configuring next.config.js to optimize how Turbopack handles external packages, specifically by adjusting serverExternalPackages or exploring other bundling options to ensure consistent class definitions across routes.

Guidance

  • Review the next.config.js file to understand how external packages are currently being handled, particularly focusing on the serverExternalPackages configuration.
  • Experiment with adding more packages to serverExternalPackages that are commonly used across routes to see if this resolves the instanceof issue.
  • Investigate if there are other Turbopack or Next.js configurations that could help in creating a shared chunk for commonly used classes, thus preserving instanceof behavior.
  • Consider the implications of using serverExternalPackages as a workaround and evaluate if this approach scales well with the project's growth.

Example

No specific code example is provided due to the complexity and variability of project configurations. However, the general approach would involve modifying the next.config.js file to include necessary packages in serverExternalPackages, like so:

module.exports = {
  //... other configurations
  serverExternalPackages: [
    // Add packages here that should be excluded from Turbopack's automatic inlining
    'aws-sdk',
    // Other packages...
  ],
}

Notes

  • The provided workaround of adding AWS SDK clients to serverExternalPackages may not be ideal for all projects due to potential performance implications or limitations in handling a large number of external packages.
  • Further investigation into Turbopack's configuration options or exploring alternative bundling strategies might yield a more scalable solution.

Recommendation

  • Apply workaround: Adjusting serverExternalPackages in next.config.js seems to be the most straightforward approach to mitigate the issue, given the current information. This method allows for explicit control over how external packages are handled during the build process.

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