NextJs: The Serverless Function exceeds the maximum size limit of 50mb

Viewed 2705

I'm new working with NextJs and when trying to deploy my project to Vercel I'm getting the following error:

Error! The Serverless Function "api/auth" is 50.55mb which exceeds the maximum size limit of 50mb.

I have spent a lot of my time trying to find a proper answer but I didn't find any. Here is the code of the api request I'm making:

const { auth: adminAuth } = require("firebase/admin");

export default async function auth(req, res) {
  const tokenId = req.query.token;
  return new Promise((resolve) => {
    adminAuth
      .verifyIdToken(tokenId)
      .then((user) => {
        res.json(user);
        resolve();
      })
      .catch(() => {
        res.status(302).send("Invalid authentication");
        resolve();
      });
  });
}

I'll be really grateful if anybody can help me, thanks y'all!

3 Answers

I've been dealing with the same issue. It appears that when bundling the serverless function vercel is pulling in ALL assets within your project. So 50.55MB is likely the size of your current entire build. I'm researching how to only include certain files within the vercel.json but have so far not figured exactly how to do that. For now you could probably just remove a few files from your public assets to get under the limit.

This is likely caused by firebase/admin including everything in the firebase package, not just the "admin" parts.

You can verify this by creating a file with only the import and running @vercel/nft to trace the files.

npm init -y
npm add firebase
echo "const { auth: adminAuth } = require('firebase/admin')" > index.js
npm i -g @vercel/nft
nft build index.js
# inspect the "dist" directory

The entire firebase package is quite large, so its best to follow the recommendation from the firebase team and use the firebase-admin package inside Serverless Functions.

This SDK (firebase) is intended for end-user client access from environments such as the Web, mobile Web (e.g. React Native, Ionic), Node.js desktop (e.g. Electron), or IoT devices running Node.js. If you are instead interested in using a Node.js SDK which grants you admin access from a privileged environment (like a server), you should use the Firebase Admin Node.js SDK (firebase-admin).

source: firebase NPM

Related