Next js Fast API . A Serverless Function has exceeded max size

I’ve deployed my project and get a cryptic error:
Error: A Serverless Function has exceeded the unzipped maximum size of 250 MB. : Troubleshooting Build Error: “Serverless Function has exceeded the unzipped maximum size of 250 MB”

-No information as to which function in particular .

I’ve added outputfiletracing to the next.config- don’t know which paths to exclude because i can’t see where the issue is. The log on build is:

./components/ui/related_articles.tsx
34:9  Warning: Using `<img>` could result in slower LCP and higher bandwidth. Consider using `<Image />` from `next/image` to automatically optimize images. This may incur additional usage or cost from your provider. See: https://nextjs.org/docs/messages/no-img-element  @next/next/no-img-element
info  - Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/app/api-reference/config/eslint#disabling-rules
   Collecting page data ...
   Generating static pages (0/23) ...
generating sitemap data
   Generating static pages (5/23) 
sitemap data generated successfully
   Generating static pages (11/23) 
   Generating static pages (17/23) 
 ✓ Generating static pages (23/23)
   Finalizing page optimization ...
   Collecting build traces ...
Route (app)                                             Size  First Load JS
┌ ○ /                                                10.4 kB         191 kB
├ ○ /_not-found                                        990 B         103 kB
├ ○ /about                                           5.74 kB         149 kB
└ ○ /sitemap.xml                                       141 B         102 kB
+ First Load JS shared by all                         102 kB
  ├ chunks/1684-99765a90fe2aba6a.js                  46.4 kB
  ├ chunks/4bd1b696-bf14931f48e91d91.js              53.2 kB
  └ other shared chunks (total)                      2.02 kB
ƒ Middleware                                         33.2 kB
○  (Static)   prerendered as static content
ƒ  (Dynamic)  server-rendered on demand
Traced Next.js server files in: 59.794ms
Created all serverless functions in: 170.631ms
Collected static files (public/, static/, .next/static): 18.071ms
Installing required dependencies...
Build Completed in /vercel/output [2m]
Deploying outputs...
Error: A Serverless Function has exceeded the unzipped maximum size of 250 MB. : https://vercel.link/serverless-function-size

My config file:

/** @type {import('next').NextConfig} */

const withBundleAnalyzer = require('@next/bundle-analyzer')({
  enabled: process.env.ANALYZE === 'true',
});
const nextConfig = { 
  
      outputFileTracingExcludes: { '/': ['.git', '.next/cache'],},
    
  
  rewrites: async () => {
    return [
    
      {
        source: "/api/:path*",
        destination:
          process.env.NODE_ENV === "development"
            ? "http://127.0.0.1:8000/api/:path*"
            : "/api/",
      },
      {
        source: "/docs",
        destination:
          process.env.NODE_ENV === "development"
            ? "http://127.0.0.1:8000/docs"
            : "/api/docs",
      },
      {
        source: "/openapi.json",
        destination:
          process.env.NODE_ENV === "development"
            ? "http://127.0.0.1:8000/openapi.json"
            : "/api/openapi.json",
      },
    ];
  },
};

module.exports =withBundleAnalyzer( nextConfig);

Can anyone help with this?

Hi,

I have checked the build logs, and can observe that the path api/index is the reason behind this “A Serverless Function has exceeded the unzipped maximum size of 250 MB” error.

Can you try excluding this path and see if it fixes your build issue?

Yes that’s where the serverless functions are stored.

My next js directory looks like:
api
|->pycache(folder)
| |->index.cpython39 etc
| -index.py
app
|->aboutus (folder)
| |->page.ts
|–> other_pages(folder)
|->pages.ts
Index.py is where all the endpoints are, it’s just standard endpoints:



import asyncio

app = FastAPI()

leaderboardsdata = {}
@app.get("/", status_code=status.HTTP_200_OK)
def yapa():
    return {'hello' : 'wanderer'} 

def format_date(date_string):
      try:
        dt = datetime.fromisoformat(date_string.replace('Z', '+00:00'))  # Handles 'Z' timezone
        return dt.strftime("%B %Y")  # Formats as "Month Year"
      except ValueError:
        return None  # Or handle the error as needed

//more endpoints..

Also it builds locally. Just doesn’t build on vercel somehow.

You may need to split that into multiple files so it gets deployed as multiple Lambdas, which should then keep it under the limit

2 Likes

Since none of my individual files exceed 1MB, I find it hard to believe the API backend’s size is the issue. Given that it’s not even running, the idea that my endpoints could account for a 250MB build seems improbable. I’m currently lacking any information on what’s causing this and would appreciate guidance on how to investigate the size contribution of each endpoint, including any relevant debugging tools. I’ve searched the net for days and tried everything recommened including excluding routes from the build etc

1 Like