Skip to content

Turbopack tree-shaking generates invalid output for some pages router SSR chunks #85172

@marcelltoth

Description

@marcelltoth

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/*',
    ],
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    OutputRelated to the the output configuration option.TurbopackRelated to Turbopack with Next.js.linear: turbopackConfirmed issue that is tracked by the Turbopack team.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions