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

Most upvoted comments

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.1

Downgrading from v0.33.2 -> v0.33.1 did not work for me.

[18:44:18.140] 
[18:44:18.302]    ▲ Next.js 14.0.4
[18:44:18.302] 
[18:44:18.302]    Creating an optimized production build ...
[18:44:30.903]  ✓ Compiled successfully
[18:44:30.905]    Linting and checking validity of types ...
[18:44:35.451]    Collecting page data ...
[18:44:36.898]    Generating static pages (0/7) ...
[18:44:37.092] 
   Generating static pages (1/7) 
[18:44:37.138] 
   Generating static pages (3/7) 
[18:44:37.780] 
   Generating static pages (5/7) 
[18:44:37.955] 
 ✓ Generating static pages (7/7) 
[18:44:38.180]    Finalizing page optimization ...
[18:44:38.180]    Collecting build traces ...
[18:44:45.179] RangeError: Maximum call stack size exceeded
[18:44:45.180]     at parse (/vercel/path0/node_modules/next/dist/compiled/micromatch/index.js:15:6313)
[18:44:45.180]     at picomatch.makeRe (/vercel/path0/node_modules/next/dist/compiled/micromatch/index.js:15:21670)
[18:44:45.180]     at picomatch (/vercel/path0/node_modules/next/dist/compiled/micromatch/index.js:15:19637)
[18:44:45.181]     at /vercel/path0/node_modules/next/dist/compiled/micromatch/index.js:15:19294
[18:44:45.181]     at Array.map (<anonymous>)
[18:44:45.181]     at picomatch (/vercel/path0/node_modules/next/dist/compiled/micromatch/index.js:15:19286)
[18:44:45.181]     at micromatch.isMatch (/vercel/path0/node_modules/next/dist/compiled/micromatch/index.js:15:1090)
[18:44:45.181]     at /vercel/path0/node_modules/next/dist/build/collect-build-traces.js:245:48
[18:44:45.181]     at shouldIgnore (/vercel/path0/node_modules/next/dist/build/collect-build-traces.js:75:9)
[18:44:45.182]     at /vercel/path0/node_modules/next/dist/build/collect-build-traces.js:86:23
[18:44:45.224] Error: Command "npm run build" exited with 1
[18:44:45.492] 

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

using sharp v0.33.1 resolved the issue

Agreed, going to sharp 0.33.1 solved for me as well.

@VitaliyPotapov

When attempting to locally build (npm run build) without the sharp package installed, you may encounter the following warning:

⚠ For optimal Image Optimization in production with Next.js, we strongly recommend installing the optional 'sharp' package. Simply run 'npm i sharp', and Next.js will automatically utilize it during the build process.
Read more: https://nextjs.org/docs/messages/sharp-missing-in-production

It’s worth noting that sharp is 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 of sharp for 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 💯

Downgrading from v0.33.2 -> v0.33.1 did not work for me.

[18:44:18.140] 
[18:44:18.302]    ▲ Next.js 14.0.4
[18:44:18.302] 
[18:44:18.302]    Creating an optimized production build ...
[18:44:30.903]  ✓ Compiled successfully
[18:44:30.905]    Linting and checking validity of types ...
[18:44:35.451]    Collecting page data ...
[18:44:36.898]    Generating static pages (0/7) ...
[18:44:37.092] 
   Generating static pages (1/7) 
[18:44:37.138] 
   Generating static pages (3/7) 
[18:44:37.780] 
   Generating static pages (5/7) 
[18:44:37.955] 
 ✓ Generating static pages (7/7) 
[18:44:38.180]    Finalizing page optimization ...
[18:44:38.180]    Collecting build traces ...
[18:44:45.179] RangeError: Maximum call stack size exceeded
[18:44:45.180]     at parse (/vercel/path0/node_modules/next/dist/compiled/micromatch/index.js:15:6313)
[18:44:45.180]     at picomatch.makeRe (/vercel/path0/node_modules/next/dist/compiled/micromatch/index.js:15:21670)
[18:44:45.180]     at picomatch (/vercel/path0/node_modules/next/dist/compiled/micromatch/index.js:15:19637)
[18:44:45.181]     at /vercel/path0/node_modules/next/dist/compiled/micromatch/index.js:15:19294
[18:44:45.181]     at Array.map (<anonymous>)
[18:44:45.181]     at picomatch (/vercel/path0/node_modules/next/dist/compiled/micromatch/index.js:15:19286)
[18:44:45.181]     at micromatch.isMatch (/vercel/path0/node_modules/next/dist/compiled/micromatch/index.js:15:1090)
[18:44:45.181]     at /vercel/path0/node_modules/next/dist/build/collect-build-traces.js:245:48
[18:44:45.181]     at shouldIgnore (/vercel/path0/node_modules/next/dist/build/collect-build-traces.js:75:9)
[18:44:45.182]     at /vercel/path0/node_modules/next/dist/build/collect-build-traces.js:86:23
[18:44:45.224] Error: Command "npm run build" exited with 1
[18:44:45.492] 

EDIT: I had "sharp": "^0.33.1" but changing it to "sharp": "0.33.1" worked.

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 sharp from our package.json? Go to the docs and see the note below:

Note: This is not necessary for Vercel deployments, since sharp is installed automatically for you.

So do we even need to have sharp installed manually?

EDIT: I got rid of sharp and it solved the problem. Command to run:

npm uninstall sharp

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

I understand that downgrading to sharp version 0.33.1 resolves the issue. However, looking at the commit history for 0.33.2, it only shows documentation changes. Why would this cause such a problem?

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.