Skip to content

Conversation

@relsunkaev
Copy link
Contributor

Summary

Fixes runtime errors when deploying Next.js 16 apps built with Turbopack to AWS Lambda.

Problem

When building with Turbopack (default in Next.js 16), the standalone output only includes *-turbo.runtime.prod.js files (e.g., pages-turbo.runtime.prod.js), not the webpack variants (pages.runtime.prod.js).

The previous behavior unconditionally set TURBOPACK=false in module.compiled.js, which caused it to try loading non-existent runtime files at runtime:

Error: Cannot find module 'next/dist/compiled/next-server/pages.runtime.prod.js'
Require stack:
- /var/task/node_modules/next/dist/server/route-modules/pages/module.compiled.js
- /var/task/node_modules/next/dist/server/route-modules/pages/vendored/contexts/router-context.js
- /var/task/node_modules/next/dist/client/compat/router.js

Solution

This PR detects whether the build was done with Turbopack by checking the traced files:

  • If only *-turbo.runtime.prod.js files exist → Turbopack build → set TURBOPACK=true
  • If non-turbo runtime files exist → Webpack build → set TURBOPACK=false

Testing

Tested with a Next.js 16.0.7 app built with Turbopack and deployed to AWS Lambda via SST. Before the fix: 502 errors. After the fix: 200 OK.

@changeset-bot
Copy link

changeset-bot bot commented Dec 5, 2025

⚠️ No Changeset found

Latest commit: 91dbdd3

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 5, 2025

Open in StackBlitz

npm i https://pkg.pr.new/@opennextjs/aws@1056

commit: 8e8a97e

When building with Turbopack (default in Next.js 16), the standalone output
only includes *-turbo.runtime.prod.js files, not the webpack variants. The
previous behavior of unconditionally setting TURBOPACK=false caused runtime
errors because module.compiled.js would try to load non-existent runtime files.

This change:
- Detects Turbopack builds by checking if only turbo runtime files exist in
  the traced files
- Sets TURBOPACK=true for Turbopack builds so the correct runtime is loaded
- Keeps TURBOPACK=false for Webpack builds (existing behavior)

Fixes runtime error: Cannot find module 'next/dist/compiled/next-server/pages.runtime.prod.js'
@relsunkaev relsunkaev force-pushed the fix/turbopack-runtime-detection branch from 8e8a97e to 91dbdd3 Compare December 6, 2025 01:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant