nextjs - ✅(Solved) Fix Turbopack tree-shaking generates invalid output for some pages router SSR chunks [1 pull requests, 13 comments, 8 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#85172Fetched 2026-04-08 02:17:11
View on GitHub
Comments
13
Participants
8
Timeline
31
Reactions
3
Timeline (top)
commented ×13labeled ×4referenced ×4subscribed ×4

Error Message

  1. Observe the 500 Internal server error. The current is that it renders a 500 error page and prints the following into the terminal:

⨯ Error: Failed to load external module aws-amplify: Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:<project-location>.next\standalone\node_modules@aws-amplify\core\dist\esm\providers\pinpoint\types\errors.mjs' imported from C:<project-location>.next\standalone\node_modules@aws-amplify\core\dist\esm\ServiceWorker\ServiceWorker.mjs

Fix Action

Fix / Workaround

Forcing the file to be included by adding this to next.config.ts acts as a valid workaround:

outputFileTracingIncludes: {
    '/*': [
      'node_modules/@aws-amplify/core/dist/esm/providers/pinpoint/types/*',
    ],
  }

PR fix notes

PR #85722: Turbopack: disable tree shaking for tracing

Description (problem / solution / changelog)

We had tree shaking enabled for NFT. That is incorrect however, as even unused imports (if you don't use any of the imported bindings, and the file is marked as sideEffects: false) are still needed at runtime, since Node.js will execute everything anyway.

  • I made the treeShakingMode: None explicit now (though that was already the Default::default anyway)
  • The real problem was that we still dropped side-effect free ModulePart::Evaluation references even when tree shaking was disabled.

Closes #85172 Fixes PACK-5756

Changed files

  • test/production/standalone-mode/tracing-side-effects-false/app/layout.js (added, +8/-0)
  • test/production/standalone-mode/tracing-side-effects-false/app/page.js (added, +5/-0)
  • test/production/standalone-mode/tracing-side-effects-false/foo/index.js (added, +6/-0)
  • test/production/standalone-mode/tracing-side-effects-false/foo/package.json (added, +6/-0)
  • test/production/standalone-mode/tracing-side-effects-false/foo/side-effect.js (added, +1/-0)
  • test/production/standalone-mode/tracing-side-effects-false/foo/value.js (added, +1/-0)
  • test/production/standalone-mode/tracing-side-effects-false/next.config.js (added, +8/-0)
  • test/production/standalone-mode/tracing-side-effects-false/package.json (added, +5/-0)
  • test/production/standalone-mode/tracing-side-effects-false/tracing-side-effects-false.test.ts (added, +36/-0)
  • turbopack/crates/turbopack-ecmascript/benches/references.rs (modified, +5/-1)
  • turbopack/crates/turbopack-ecmascript/src/lib.rs (modified, +1/-1)
  • turbopack/crates/turbopack-nft/src/nft.rs (modified, +7/-2)
  • turbopack/crates/turbopack-tracing/tests/node-file-trace.rs (modified, +8/-2)
  • turbopack/crates/turbopack-tracing/tests/unit.rs (modified, +8/-2)
  • turbopack/crates/turbopack/src/lib.rs (modified, +14/-6)

Code Example

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Pro N
  Available memory (MB): 64630
  Available CPU cores: 24
Binaries:
  Node: 20.9.0
  npm: 10.1.0
  Yarn: 1.22.22
  pnpm: 10.17.1
Relevant Packages:
  next: 16.0.0-canary.16 // Latest available version is detected (16.0.0-canary.16).
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: 5.9.3
Next.js Config:
  output: standalone

---

outputFileTracingIncludes: {
    '/*': [
      'node_modules/@aws-amplify/core/dist/esm/providers/pinpoint/types/*',
    ],
  }
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/marcelltoth/turbopack-optimization-bug-demo

To Reproduce

Take a look at the linked repository.

  1. Build the project - npm run build
  2. Copy the standalone files so we can run it locally without a CDN - cp -r .next/static .next/standalone/.next/
  3. Run the standalone artifact - HOSTNAME=localhost node .next/standalone/server.js
  4. Visit http://localhost:3000
  5. Observe the 500 Internal server error.

Current vs. Expected behavior

Expected behavior is that the (empty) page loads successfully.

The current is that it renders a 500 error page and prints the following into the terminal:

⨯ Error: Failed to load external module aws-amplify: Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:<project-location>.next\standalone\node_modules@aws-amplify\core\dist\esm\providers\pinpoint\types\errors.mjs' imported from C:<project-location>.next\standalone\node_modules@aws-amplify\core\dist\esm\ServiceWorker\ServiceWorker.mjs at Context.externalImport [as y] (C:<project-location>.next\standalone.next\server\chunks\ssr[turbopack]runtime.js:497:15) at async (C:<project-location>.next\standalone.next\server\chunks\ssr[root-of-the-server]__3e1fdc6a..js:1:52)

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Pro N
  Available memory (MB): 64630
  Available CPU cores: 24
Binaries:
  Node: 20.9.0
  npm: 10.1.0
  Yarn: 1.22.22
  pnpm: 10.17.1
Relevant Packages:
  next: 16.0.0-canary.16 // Latest available version is detected (16.0.0-canary.16).
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: 5.9.3
Next.js Config:
  output: standalone

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

Turbopack, Output

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

next build (local)

Additional context

I narrowed it down to the following:

  1. It only happens with Turbopack prod builds. Not with Webpack prod builds or Turbopack dev builds.
  2. I wasn't able to reproduce the issue with the App router (server or client components), or with static pregenerated pages router pages. It has to be an SSR chunk, which is why I added the empty getInitialProps to the example.
  3. It happens both with current stable and canary.

The issue seems to be the tree-shaking behavior of Turbopack, I found this by looking at the build output and node_modules.

  1. If you look at the input file node_modules/@aws-amplify/core/dist/esm/ServiceWorker/ServiceWorker.mjs you can see it does have the import import '../providers/pinpoint/types/errors.mjs';
  2. The package.json for @aws-amplify/core says sideEffects is only true for 4 particular files, that does not include the above.
  3. Probably for this reason Turbopack decides to drop node_modules\@aws-amplify\core\dist\esm\providers\pinpoint\types\errors.mjs from the output.
  4. At the same time it fails to drop the import itself, which will result in the failure we're seeing.

Forcing the file to be included by adding this to next.config.ts acts as a valid workaround:

outputFileTracingIncludes: {
    '/*': [
      'node_modules/@aws-amplify/core/dist/esm/providers/pinpoint/types/*',
    ],
  }

extent analysis

TL;DR

The issue can be resolved by configuring outputFileTracingIncludes in next.config.ts to include the missing @aws-amplify/core files.

Guidance

  • The problem is caused by Turbopack's tree-shaking behavior, which drops the errors.mjs file from the output, but not the import statement.
  • To fix this, add the following configuration to next.config.ts:
outputFileTracingIncludes: {
  '/*': [
    'node_modules/@aws-amplify/core/dist/esm/providers/pinpoint/types/*',
  ],
}
  • This will force Turbopack to include the necessary files in the output.
  • Verify the fix by rebuilding the project and running it locally to ensure the 500 error is resolved.

Example

The provided workaround code snippet is a good example of how to configure outputFileTracingIncludes:

outputFileTracingIncludes: {
  '/*': [
    'node_modules/@aws-amplify/core/dist/esm/providers/pinpoint/types/*',
  ],
}

This code tells Turbopack to include all files in the node_modules/@aws-amplify/core/dist/esm/providers/pinpoint/types directory.

Notes

This fix may need to be adjusted if the @aws-amplify/core package changes its internal structure or if other packages have similar issues.

Recommendation

Apply the workaround by adding the outputFileTracingIncludes configuration to next.config.ts, as this is a targeted fix for the specific issue with Turbopack and @aws-amplify/core.

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

nextjs - ✅(Solved) Fix Turbopack tree-shaking generates invalid output for some pages router SSR chunks [1 pull requests, 13 comments, 8 participants]