'Next.js API routes all return "Interval Server Error" in production, but all the routes work in development

Whenever I go to any API route in my Next.js app in production it returns a 500 "Internal Server Error" but in development, all of them work completely fine and show/return what I expect them to.

another example of the error

the error

I am deploying with an AWS Ec2 instance.

the code is available here: https://github.com/123om123/NFT-Marketplace

These are all my API routes.

enter image description here

The [...auth0].js creates the following routes: /api/auth/login, /api/auth/logout, /api/auth/callback, and /api/auth/me

If I try to access the "find_account" API route like the following:

  let findAccount = async function () {
    await fetch("/api/find_account", {
      method: "POST",
      body: JSON.stringify({
        DBUrl: DBUrl,
        user_id: user.sub,
      }),
    })
      .then(async (response) => {
        await response.json().then((result) => {
          accountData = result;
          if (accountData.data.allAccounts.nodes[0].addresses !== null) {
            setAddressList(accountData.data.allAccounts.nodes[0].addresses[0].split(","));
          }
        });
      })
      .catch((err) => {
        return err;
      });
  };

which handles requests like the following:

export default function handler(req, res) {
  req.body = JSON.parse(req.body);
  fetch(req.body.DBUrl, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      query: `query MyQuery {
        allAccounts(condition: {userId:"${req.body.user_id}"}) {
          nodes {
            addresses
          }
        }
      }`,
    }),
  })
    .then((response) => {
      response.json().then((response) => {
        res.status(200).send(response);
      });
    })
    .catch((err) => {
      res.status(500).send(err);
    });
}

it works fine and returns the response from the graphql API in development, but in production it shows the above error.

The problem seems to be that the API routes aren't even created and are therefore inaccessible. All the API routes worked a few weeks ago, but now they seem to have stopped working.



Solution 1:[1]

Check out this answer, it helped me solve a bug similar to yours. In my case when vercel was deploying my PR branch I was getting a 500 Internal Error. Once I merge the PR into main and vercel does a fresh deployment, I was now getting a 504 gateway error. The answer below helped me find out why.

https://stackoverflow.com/a/68774331/12775824

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RyanK