next.js: Standalone output + outputFileTracingExcludes: RangeError: Maximum call stack size exceeded
Link to the code that reproduces this issue
https://github.com/revmischa/next-repro-outputFileTracingExcludes
To Reproduce
I have a couple medium-sized Next projects, one pages and one app router. My output bundle contains many dev dependencies like webpack and sass and esbuild. I tried adding patterns to remove them using:
outputFileTracingExcludes: {
"*": [
"node_modules/@esbuild*",
"node_modules/sass*",
]
}
and many variants thereof. This works okay but if I do
outputFileTracingExcludes: {
"*": [
"node_modules/@esbuild*",
"node_modules/sass*",
"node_modules/webpack*",
]
}
Then I get this error:
> my-app@0.1.0 build /Users/cyber/dev/next-tracing-excludes/my-app
> next build
▲ Next.js 14.0.4
- Experiments (use at your own risk):
· outputFileTracingRoot
✓ Creating an optimized production build
✓ Compiled successfully
✓ Linting and checking validity of types
✓ Collecting page data
✓ Generating static pages (5/5)
RangeError: Maximum call stack size exceeded
at String.replace (<anonymous>)
at parse (/Users/cyber/dev/next-tracing-excludes/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/micromatch/index.js:15:9078)
at picomatch.makeRe (/Users/cyber/dev/next-tracing-excludes/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/micromatch/index.js:15:21670)
at picomatch (/Users/cyber/dev/next-tracing-excludes/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/micromatch/index.js:15:19637)
at /Users/cyber/dev/next-tracing-excludes/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/micromatch/index.js:15:19294
at Array.map (<anonymous>)
at picomatch (/Users/cyber/dev/next-tracing-excludes/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/micromatch/index.js:15:19286)
at micromatch.isMatch (/Users/cyber/dev/next-tracing-excludes/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/micromatch/index.js:15:1090)
at /Users/cyber/dev/next-tracing-excludes/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/build/collect-build-traces.js:245:48
at shouldIgnore (/Users/cyber/dev/next-tracing-excludes/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/build/collect-build-traces.js:75:9)
at /Users/cyber/dev/next-tracing-excludes/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/build/collect-build-traces.js:86:23
at Array.every (<anonymous>)
at shouldIgnore (/Users/cyber/dev/next-tracing-excludes/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/build/collect-build-traces.js:86:7)
at /Users/cyber/dev/next-tracing-excludes/node_modules/.pnpm/next@14.0.4_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/build/collect-build-traces.js:86:23
at Array.every (<anonymous>)
It happens regardless of what kind of minimatch patterns I specify, even if there are no wildcards in the patterns.
Current vs. Expected behavior
I expect build to complete without an error.
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 23.1.0: Mon Oct 9 21:32:11 PDT 2023; root:xnu-10002.41.9~7/RELEASE_ARM64_T6030
Binaries:
Node: 20.10.0
npm: 10.2.3
Yarn: 1.22.17
pnpm: 8.8.0
Relevant Packages:
next: 14.0.5-canary.41
eslint-config-next: 14.0.4
react: 18.2.0
react-dom: 18.2.0
typescript: 5.3.3
Next.js Config:
output: standalone
### Which area(s) are affected? (Select all that apply)
Standalone mode (output: "standalone")
### Which stage(s) are affected? (Select all that apply)
next build (local)
### Additional context
A few more notes here: https://github.com/vercel/next.js/issues/42641#issuecomment-1872592551
About this issue
- Original URL
- State: closed
- Created 6 months ago
- Reactions: 7
- Comments: 40 (3 by maintainers)
Commits related to this issue
- chore: pin sharp to 0.33.1 ref: https://github.com/vercel/next.js/issues/60301 — committed to zce/velite by zce 5 months ago
- fix: deploy on vercel https://github.com/vercel/next.js/issues/60301 — committed to Coloby/Personal-Blog by Coloby 4 months ago
using sharp v0.33.1 resolved the issue
Found it, vercel build fails if I update “sharp”: “0.33.1” -> “sharp”: “0.33.2”,
Yess, that fixed it. To pin to
v0.33.1Downgrading from v0.33.2 -> v0.33.1 did not work for me.
EDIT: I had
"sharp": "^0.33.1"but changing it to"sharp": "0.33.1"worked.In my case, next@14.1.0 fixed it with sharp@0.33.2. https://github.com/vercel/next.js/pull/60740
Agreed, going to sharp 0.33.1 solved for me as well.
@VitaliyPotapov
When attempting to locally build (
npm run build) without thesharppackage installed, you may encounter the following warning:It’s worth noting that
sharpis specifically utilized during production builds (npm run build), and it is not necessary for the development server (npm run dev). Additionally, if you plan to deploy your application using Vercel, there’s no need to worry as Vercel automatically handles the installation ofsharpfor you.This problem appears to be caused by uncontrolled recursive behaviour within Next.js.
https://github.com/vercel/next.js/blob/dc394483b807e5e111c175ad9afd1f09220da7ce/packages/next/src/build/collect-build-traces.ts#L59
Pinning dependencies is merely perendinating the problem.
Setting
"sharp": "0.33.1"to exact version works with me also 💯yep, you have to set the exact version
Thanks, @a-rustacean, for figuring it out. It did help!
14.1.0 works for me!
Should we just remove
sharpfrom ourpackage.json? Go to the docs and see the note below:So do we even need to have
sharpinstalled manually?EDIT: I got rid of
sharpand it solved the problem. Command to run:Acts as a clean-up too, removing unnecessary packages.
PS: Regarding the diff, these two files could be the potential root cause: lib/libvips.js, lib/sharp.js
It’s not only documentation change per diff 0.33.1 … 0.33.2
For those who hadn’t seen, https://github.com/vercel/next.js/pull/60740 (v14.0.5-canary.60) removed the potential for uncontrolled recursion in the Next.js build logic.
I’m unsure exactly why sharp v0.33.2 tipped it over the edge of the call stack limit, but from looking at the change, other dependencies/upgrades could easily have done the same.