Unused constants from constants.js do not get tree-shaked in the client bundles
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 142k
- Forks
- 32.5k
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 351
Description
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.
- Clone the repo above;
- Run
yarn build - Inspect the bundles (especially .next/static/chunks/main-xxx.js); observe that unused constants are imported in the client bundle
- 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 thatnode_modules/next/dist/shared/lib/constants.jsandnode_modules/next/dist/lib/constants.jsare 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:
- https://github.com/vercel/next.js/blob/f50ef62c54e36cd52684a72630531a87d846767b/packages/next/src/shared/lib/router/utils/route-regex.ts#L1-L4
- https://github.com/vercel/next.js/blob/e84b65b0d93f29ed26ef898e980a7e2aca2dae98/packages/next/src/shared/lib/router/router.ts#L47
- https://github.com/vercel/next.js/blob/1e332532c7e0036de2e09802b83eb7d692b2d654/packages/next/src/client/page-loader.ts#L12-L15
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with the linked demo using yarn build, then inspect .next/static/chunks/main-xxx.js for the constants modules and unused exports. Read the referenced route-regex.ts, router.ts, and page-loader.ts changes alongside the client bundle behavior. Done means production client bundles exclude constants that are not used by the client.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, nextjs, react
- Domain
- build-system, frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100