nextjs - 💡(How to fix) Fix When using Next.js with Turbopack and SWC, errors occur during compression of third‑party library code. [2 comments, 3 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#86568Fetched 2026-04-08 02:10:19
View on GitHub
Comments
2
Participants
3
Timeline
9
Reactions
1
Timeline (top)
referenced ×3commented ×2labeled ×2issue_type_added ×1

Error Message

I extracted the part of the real project where the error occurred, and the same issue can be reproduced. The reproduction steps are as follows. Exception thrown, error in console.

Code Example

Operating System:
Platform: windows
Version: Windows 11

Binaries: 
Node: 20.15.1
npm:10.9.0
pnpm:9.4.0

Relevant Packages:
    "next": "16.0.4",
    "react": "19.2.0",
    "react-dom": "19.2.0"
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/branderzhai/bug/tree/main/nextjs_swc_issue

To Reproduce

When building a project with the default Turbopack,SWC will re-compress the code of third‑party libraries during bundling. However, due to a bug in SWC, the compressed code becomes semantically inequivalent to the original.

I extracted the part of the real project where the error occurred, and the same issue can be reproduced. The reproduction steps are as follows.

nextjs_swc_issue.zip Reproduce Steps

  1. unzip the demo.
  2. pnpm i
  3. pnpm build
  4. pnpm start
  5. clicking the button

Exception thrown, error in console.

Current vs. Expected behavior

As you can see, the original code (or use "pnpm dev" to run up the project) like below

<img width="1365" height="117" alt="Image" src="https://github.com/user-attachments/assets/7646bda6-41c9-44eb-8e45-299015ee5f06" />

However, after the SWC process, an internal method call was inlined into the main function, but its variable names were not handled correctly. <img width="887" height="252" alt="Image" src="https://github.com/user-attachments/assets/ed079006-0471-4641-805d-80b9bf9c25da" />


When I replaced the default build tool with webpack and terser, everything returned to normal.

Provide environment information

Operating System:
Platform: windows
Version: Windows 11

Binaries: 
Node: 20.15.1
npm:10.9.0
pnpm:9.4.0

Relevant Packages:
    "next": "16.0.4",
    "react": "19.2.0",
    "react-dom": "19.2.0"

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

Turbopack, SWC

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

next build (local)

Additional context

No response

extent analysis

TL;DR

The issue can be worked around by replacing the default build tool with webpack and terser, as this resolves the semantic equivalence issue caused by SWC.

Guidance

  • The problem seems to stem from SWC's code compression altering the semantic meaning of the original code, specifically with variable names in inlined method calls.
  • To verify the issue, follow the reproduction steps provided and compare the behavior with the expected output.
  • Replacing Turbopack with webpack and using terser for compression, as mentioned, seems to resolve the issue, indicating a potential problem with SWC's handling of variable names during compression.
  • Consider testing the build process with different compression tools or configurations to isolate the root cause of the semantic equivalence issue.

Example

No specific code example is provided due to the complexity of the issue and the need for a comprehensive understanding of the project structure and build process.

Notes

The solution of using webpack and terser is based on the information provided in the issue and may not be applicable in all scenarios. Further investigation into SWC's compression algorithm and its handling of variable names during inlining might be necessary for a more targeted fix.

Recommendation

Apply workaround: Replace the default build tool with webpack and terser, as this has been shown to resolve the issue in the provided test case. This approach allows for a functional workaround while the root cause of the SWC issue is further investigated.

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