'Next js api not works on netlify

I have a web application for next js. I want to put it on netlify, but when I do this, the api stops working for me, although everything worked fine on vercel. When i call api a get 404 errorenter image description here

package.json:

{
  "name": "devhub-website",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "export": "next export"
  },
  "dependencies": {
    "axios": "^0.19.2",
    "dotenv": "^8.2.0",
    "js-cookie": "^2.2.1",
    "jsonwebtoken": "^8.5.1",
    "mongodb": "^3.6.0",
    "next": "9.5.2",
    "react": "16.13.1",
    "react-dom": "16.13.1",
    "sass": "^1.26.10"
  },
  "devDependencies": {
    "@types/node": "^14.0.27",
    "@types/react": "^16.9.46",
    "typescript": "^3.9.7"
  }
}


Solution 1:[1]

Make sure you set the target to serverless in next.config.js

The Next.js serverless target outputs Serverless functions from pages.

module.exports = {
  target: "serverless",
};

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 Elvin Chu