nextjs - 💡(How to fix) Fix Unused constants from constants.js do not get tree-shaked in the client bundles [1 comments, 2 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#85664Fetched 2026-04-08 02:14:38
View on GitHub
Comments
1
Participants
2
Timeline
4
Reactions
2
Timeline (top)
commented ×1issue_type_added ×1labeled ×1subscribed ×1

Root Cause

I think the exact reason for this issue is that, because you are "transpiling" all these files, and pointing all client entrypoints to, CJS (and not ESM) by default, which negates the possibility of tree-shaking.

Code Example

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 22.6.0: Tue Jul 15 08:22:28 PDT 2025; root:xnu-8796.141.3.713.2~2/RELEASE_X86_64
  Available memory (MB): 8192
  Available CPU cores: 4
Binaries:
  Node: 24.7.0
  npm: 11.5.1
  Yarn: 1.22.22
  pnpm: 10.19.0
Relevant Packages:
  next: 16.0.2-canary.3 // Latest available version is detected (16.0.2-canary.3).
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: N/A
Next.js Config:
  output: N/A
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/seyoon20087/nextjs-constants-not-treeshaked-demo

To Reproduce

NOTE: This actually happens on all Next.js apps that use the current version at the time of this writing; but still I am reporting it here.

  1. Clone the repo above;
  2. Run yarn build
  3. Inspect the bundles (especially .next/static/chunks/main-xxx.js); observe that unused constants are imported in the client bundle
  4. To additionally confirm this, I ran yarn client:rsd, opened the link printed in the terminal, then go to the "bundle size" section (next to "compile analysis". I see that, when inspecting the "static/chunls/main-xxx.js" file deeply, I see that node_modules/next/dist/shared/lib/constants.js and node_modules/next/dist/lib/constants.js are included in the bundle but does not tree-shake any constants that are not actually used in the client

Current vs. Expected behavior

Expected behavior:

Unused constants get tree-shaked out from the production bundle

Current behavior:

Unused constants ends up in the production bundle

Provide environment information

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 22.6.0: Tue Jul 15 08:22:28 PDT 2025; root:xnu-8796.141.3.713.2~2/RELEASE_X86_64
  Available memory (MB): 8192
  Available CPU cores: 4
Binaries:
  Node: 24.7.0
  npm: 11.5.1
  Yarn: 1.22.22
  pnpm: 10.19.0
Relevant Packages:
  next: 16.0.2-canary.3 // Latest available version is detected (16.0.2-canary.3).
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: N/A
Next.js Config:
  output: N/A

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

Output

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

next build (local)

Additional context

This issue started all the way back from these commits:

I think the exact reason for this issue is that, because you are "transpiling" all these files, and pointing all client entrypoints to, CJS (and not ESM) by default, which negates the possibility of tree-shaking.

extent analysis

TL;DR

The issue can be addressed by exploring alternatives to the current transpilation approach that prevents tree-shaking of unused constants in the production bundle.

Guidance

  • Investigate the possibility of using ES modules (ESM) instead of CommonJS (CJS) for client entrypoints to enable tree-shaking.
  • Review the commits mentioned in the additional context to understand the changes that led to this issue and consider potential workarounds.
  • Consider configuring the build process to use a different module system or transpilation approach that supports tree-shaking.
  • Verify the effectiveness of any changes by inspecting the bundles and checking the bundle size analysis.

Example

No specific code snippet is provided as the issue is related to the build configuration and transpilation approach rather than a specific code block.

Notes

The issue seems to be related to the transpilation of files to CJS, which prevents tree-shaking. Exploring alternative approaches, such as using ESM, may help resolve the issue. However, this may require significant changes to the build configuration and may have other implications.

Recommendation

Apply a workaround by exploring alternative transpilation approaches or module systems that support tree-shaking, as upgrading to a fixed version is not explicitly mentioned as an option. This is because the issue is related to the fundamental build configuration and transpilation approach rather than a specific version bug.

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