- 
                Notifications
    
You must be signed in to change notification settings  - Fork 29.7k
 
Description
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.
- Build the project - 
npm run build - Copy the standalone files so we can run it locally without a CDN - 
cp -r .next/static .next/standalone/.next/ - Run the standalone artifact - 
HOSTNAME=localhost node .next/standalone/server.js - Visit 
http://localhost:3000 - 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: standaloneWhich 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:
- It only happens with Turbopack prod builds. Not with Webpack prod builds or Turbopack dev builds.
 - 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 
getInitialPropsto the example. - 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.
- If you look at the input file 
node_modules/@aws-amplify/core/dist/esm/ServiceWorker/ServiceWorker.mjsyou can see it does have the importimport '../providers/pinpoint/types/errors.mjs'; - The 
package.jsonfor@aws-amplify/coresayssideEffectsis only true for 4 particular files, that does not include the above. - Probably for this reason Turbopack decides to drop 
node_modules\@aws-amplify\core\dist\esm\providers\pinpoint\types\errors.mjsfrom the output. - 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/*',
    ],
  }