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
@relsunkaev
Copy link
Contributor Author

@vicb Not sure what's going on with the workflow not passing but this fixed our Next 16 Turbopack deploys. Currently running this as a pnpm patch.

'${value}'
`;

function isTurbopackBuild(tracedFiles: string[]): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be nice to extract this into an helper

  • so that it can be re-used across different files
  • and ultimately moved to Next.js

We use this check in the cloudflare adapter

patchCode: createPatchCode(envVarRuleCreator("NODE_ENV", '"production"')),
},
// This patch will set `TURBOPACK` env to false to avoid loading turbopack related deps at runtime
// Turbopack builds only include *-turbo.runtime.prod.js files, so we detect and set accordingly
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I don't think we really care about how we detect a turbopack build in this comment?

Maybe revert to the inlined version of the code and remove the comment altogether when you add the helper?

Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to add an e2e test got that (i.e. identify something that is failing without this patch)

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.

2 participants