# How to deploy a Next.js app with a backend and database.

URL: https://antideploy.com/blog/how-to-deploy-a-nextjs-app-with-a-backend-and-database

A Next.js app runs as a server on Antideploy. Here is what is detected, how the database and migrations work, and what breaks builds.

## At a glance

- **next build**: Run as your build script
- **next start**: Run as your start script
- **Postgres**: With Prisma or Drizzle migrations
- **Build-time traps**: Variables and import-time code

Published 7 October 2026. Facts checked 7 October 2026. 4 min read.

## The short answer

To deploy a Next.js app with a backend and a database, give your coding agent the sentence below. Antideploy detects Next.js, runs your `build` script, starts the app with your `start` script or `next start`, and treats it as an app with a server. If your code needs Postgres, it creates a database and sets `DATABASE_URL`. A Next.js app counts as one live app on your plan.

> **Say this to your coding agent**
>
> Set this project up to deploy on Antideploy. Fetch https://antideploy.com/agent.md and follow it.

## What Antideploy detects

- **Next.js.** Detected from the `next` dependency. Nuxt, SvelteKit, Remix, Astro and Gatsby are detected the same way.
- **Build and start.** It runs `npm run build` when you have a `build` script. It starts with `npm run start`, or `next start` when there is no start script.
- **The port.** It starts the app with `PORT` set. Next.js reads it on its own.
- **The package manager.** It follows your lock file: `npm ci` for a `package-lock.json`, and `pnpm` or `yarn` with a frozen lock file.
- **The database.** A Prisma datasource, Drizzle config or Postgres driver means Postgres. Antideploy creates one and sets `DATABASE_URL`.

## Migrations

Prisma runs `npx prisma migrate deploy` when you have a migrations folder, and `npx prisma db push` when you do not. Drizzle runs `npx drizzle-kit migrate`. The command runs after the build and before the new version goes live. If it fails, the deploy fails and the broken schema is never released.

Prisma 7 removed the `url` field from the datasource block. If you see an error about it, pin `prisma` and `@prisma/client` to version 6 in `package.json`, or move the connection string into `prisma.config.ts`.

## What breaks Next.js builds

Next.js runs each route's module while it builds. Anything that throws as soon as a file is loaded takes the build down with it.

- **A variable that is not set during the build.** Secrets you save on Antideploy are available when the app runs, not while it builds. Give the value a fallback, or read it inside the handler instead of at the top of the file. See [Failed to collect page data](https://antideploy.com/blog/nextjs-failed-to-collect-page-data).
- **A database call at import time.** Opening a connection or running a query at the top of a file runs during the build. Move it inside the handler.
- **A type error.** Type errors fail the build even when the app runs locally with `next dev`. See [TypeScript errors fail the build](https://antideploy.com/blog/typescript-error-fails-the-build).
- **`NEXT_PUBLIC_` variables.** These are copied into the built files, so they must be set before the build. Never put a secret in one, because anyone who loads the site can read it.

## Step by step

### 1. Run the build on your machine first

`npm run build` should pass locally. If it fails, the deploy will too.

### 2. Paste the sentence and approve one link

Your agent creates the app, and a database if your code needs one, and writes the connection details into your `.env` without printing them.

### 3. Deploy and open the link

Antideploy installs, builds, runs your migration, starts the app and checks that it answers. Your agent tells you the address.

## Sign-in and uploads

Antideploy can switch on sign-in for your app's users. In Next.js, you install Neon's auth library and follow Neon's quickstart, and the variables it needs are already set. Uploads should go to a bucket, never to the app's disk. See [add login](https://antideploy.com/blog/how-to-add-login-to-your-app) and [add file uploads](https://antideploy.com/blog/how-to-add-file-uploads-to-your-app).

## Here's where it stops.

A platform that only tells you what it is good at is one you find the edges of in production. These are the ones to know before your first deploy.

### Apps sleep when idle

On every plan, an app nobody is using goes to sleep and the next visit wakes it. The first request took 2.9 to 13.9 seconds in our measurements, depending on the stack. There are no always-on workers, so use a cron job or a webhook for background work.

### One instance, no previews

Each app is one instance with 1 shared vCPU and 1 GB of memory, and a Java app gets 1 dedicated vCPU and 2 GB. There are no preview deployments and no per-branch URLs.

### The disk forgets

Anything written to local disk is gone on the next deploy. Apps that speak S3 get a bucket instead.

## Next.js questions, answered.

Anything else? Write to us and a person answers.

[support@antideploy.com](mailto:support@antideploy.com)

### Does Next.js need a Dockerfile?

No. Antideploy builds it from your project. If there is a Dockerfile at the root, it is used instead.

### Is a Next.js app free to host here?

It runs on the Free plan, which includes 1 live app with a server, 10 successful deploys a month and 1 Postgres database. Failed deploys are free.

### Does it support API routes and server actions?

Yes. The app runs as one long-running server, so API routes and server code work as they do locally.

### Why is the first visit slow?

Apps sleep when idle. The first request after sleeping took 2.9 to 13.9 seconds in our measurements, depending on the stack, and Next.js was the slowest stack we measured.

### Which Node.js version does it use?

The one in `engines.node`, a `.nvmrc` or a `.node-version` file. If none is set, the builder picks one.

## More guides, and the platform.

Each platform page covers one part of Antideploy: what it does, where it stops and the questions people ask. [See the whole platform](https://antideploy.com/platform), or [all the guides](https://antideploy.com/blog).

- [How to add a Postgres database to your app](https://antideploy.com/blog/how-to-add-a-postgres-database-to-your-app): Ask your agent. A Postgres 17 database is created beside your app and DATABASE_URL is set.
- [Failed to collect page data for /api/users](https://antideploy.com/blog/nextjs-failed-to-collect-page-data): A route's code threw during the build. Usually a variable or a database call at the top of a file.
- [TypeScript error fails the build](https://antideploy.com/blog/typescript-error-fails-the-build): A type error stops the build even when the app runs locally in dev mode. Run the type check and fix it.

- [Hosting](https://antideploy.com/platform/hosting)
- [Database](https://antideploy.com/platform/database)
- [Environment variables](https://antideploy.com/platform/environment-variables)
- [Authentication](https://antideploy.com/platform/authentication)
- [Deployments](https://antideploy.com/platform/deployments)

Facts on this page were checked against the live platform on 7 October 2026.

## Deploy something. Start with one sentence.

Paste one sentence into your coding agent, click Approve once, and get a live link. No card, no trial clock.

To set this up, give your coding agent this sentence:

```text
Set this project up to deploy on Antideploy. Fetch https://antideploy.com/agent.md and follow it.
```
