Next.js with NX mono repo "The function exceeds the maximum size of 250 MB"

Site ID: be0e2156-2b30-4482-8ff8-bbb9378bc355


When deploying an empty Next.js project that within NX mono repo, the deploying step keep fails because of this error:

7:53:09 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)
7:53:09 PM: The function exceeds the maximum size of 250 MB
7:53:04 PM: Deploy site                                                   
7:53:04 PM: ────────────────────────────────────────────────────────────────
7:53:04 PM: ​
7:53:04 PM: Starting to deploy site from 'apps/org/.next'
7:53:04 PM: Calculating files to upload
7:53:05 PM: 0 new file(s) to upload
7:53:05 PM: 1 new function(s) to upload
7:53:09 PM: Failed to upload file: ___netlify-server-handler
7:53:09 PM: Section completed: deploying
7:53:09 PM: ​
7:53:09 PM: Error deploying                                               
7:53:09 PM: ────────────────────────────────────────────────────────────────
7:53:09 PM: ​
7:53:09 PM:   Error message
7:53:09 PM:   Deploy did not succeed with HTTP Error 400: [PUT /deploys/{deploy_id}/functions/{name}][400] uploadDeployFunction default  &{Code:400 Message:The function exceeds the maximum size of 250 MB}
7:53:09 PM: ​
7:53:09 PM:   Error location
7:53:09 PM:   At deploy the stage with HTTP status code '400'
7:53:09 PM: ​
7:53:09 PM:   Resolved config
7:53:09 PM:   build:
7:53:09 PM:     command: nx run @org/org:build
7:53:09 PM:     commandOrigin: ui
7:53:09 PM:     publish: /opt/build/repo/apps/org/.next
7:53:09 PM:     publishOrigin: ui
7:53:09 PM:   headers:
7:53:09 PM:     - for: /_next/static/*
      values:
        Cache-Control: public, max-age=31536000, immutable
  headersOrigin: inline
  plugins:
    - inputs: {}
      origin: ui
      package: "@netlify/plugin-nextjs"
  redirects:
    - from: /_next/image
      query:
        q: :quality
        url: :url
        w: :width
      status: 200
      to: /.netlify/images?url=:url&w=:width&q=:quality
    - from: /_ipx/*
      query:
        q: :quality
        url: :url
        w: :width
      status: 200
      to: /.netlify/images?url=:url&w=:width&q=:quality
  redirectsOrigin: inline
7:53:09 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
7:53:09 PM: Failing build: Failed to build site

I’m using:

  • Nx@21.2.2
  • Next.js@15

Steps:

  • Init new Nx workspace npx create-nx-workspace@latest
  • Create a Next.js app using Nx generator
  • Upload to GitHub repo
  • Connect that repo to new Netlify site

It looks like the deploy failed because the Function bundle is going over the 50 MB zipped or 250 MB unzipped limit.

The 250 MB limit is a limit at AWS Lambda itself. There is more documentation about this here:

These are the specific limits referenced there:

50 MB (zipped, when uploaded through the Lambda API or SDKs). Upload larger files with Amazon S3.

50 MB (when uploaded through the Lambda console)

250 MB The maximum size of the contents of a deployment package, including layers and custom runtimes. (unzipped)

For this reason, Netlify cannot raise the limit.

To see why the function is so large, the recommended workflow is to build and bundle the site locally using the Netlify CLI tool using the command netlify build. There is more documentation about this here:

That will build the site and bundle the functions so you can see what size they are and what they contain.