Next js api not works on netlify

Viewed 901

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"
  }
}
2 Answers

If you are using next export to deploy in Netlify, the API routes do not work. When we build with next export, this warning shows in the terminal:

Statically exporting a Next.js application via next export disables API routes. This command is meant for static-only hosts, and is not necessary to make your application static.

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",
};
Related