[Turbopack] Duplicate class definitions across route chunks, breaking `instanceof`
Link to the code that reproduces this issue
https://github.com/TrevorBurnham/nextjs-instanceof-api-error
To Reproduce
- Clone the repo and run
npm install && npm run build - 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.
Source: vercel/next.js