Next.js

Deploy a Next.js app

Run your Next.js app in production with its own server, a managed database and environment variables set per environment. This guide goes from a new project to a production deploy on Modelence.

Last updated /Tested with Next.js 16.4 and Node.js 22

To deploy a Next.js app on Modelence, describe it in a modelence.config.json file that builds it with next build and starts it with next start, then run npx modelence@latest deploy. Modelence is a managed cloud that runs the app as a long-running Node.js server on AWS, in one of 17 regions, next to managed MongoDB, Postgres and Redis databases.

This guide starts from a new Next.js app, runs it locally, connects a database and deploys it. If you already have a Next.js app, skip to Run the app locally.

Next.js on Modelence at a glance

How it runs
next build on an official Node.js image, then next start as a long-running server on the PORT Modelence sets
Regions
17 AWS regions across the Americas, Europe and Asia Pacific, with the app and its databases in the same one
Databases
Managed MongoDB, Postgres and Redis in the app's region, passed in as MONGODB_URI, DATABASE_URL and REDIS_URL
Backups
Production databases are snapshotted at least every six hours and encrypted at rest, with nothing to set up
Deploys
New containers pass health checks before the old ones stop, and a version that keeps crashing is rolled back
Containers
From 0.25 vCPU and 0.5 GB up to 4 vCPU and 16 GB each, spread across two availability zones
Price
Free plan to start, and paid plans from $20 a month with $20 of usage included

Before you start

  • Node.js 20.9 or newer, which Next.js 16 needs, and npm
  • A Modelence account, which you can create for free
  • Optionally, Claude Code, Cursor or Codex, if you'd rather have your coding agent write the config and run the deploy

Create a Next.js app

Create a new app with create-next-app. Follow the prompts; the defaults work for this guide. The generated package.json has dev, build and start scripts that run next dev, next build and next start.

bash
npx create-next-app@latest my-app
cd my-app

Run the app locally

Start the development server and open http://localhost:3000:

npm run dev

You can also connect the project to a Modelence local environment, so the app runs on your machine with the environment's database connection strings and variables and they never need to be copied into a .env file. npx modelence@latest setup opens the browser, where you sign in and pick or create an app and environment. After that, start the app with npx modelence@latest dev instead of npm run dev. It runs your dev script with the environment's variables, and the environment's Overview in the dashboard shows the app while it runs.

bash
npx modelence@latest setup
npx modelence@latest dev

Add a database

  • MongoDBMONGODB_URI
  • PostgresDATABASE_URL
  • RedisREDIS_URL

A Next.js app reaches Modelence's managed MongoDB, Postgres and Redis through connection strings that arrive as environment variables, encrypted in transit and with production databases backed up at least every six hours.

MongoDB arrives as MONGODB_URI. Postgres and Redis arrive under the names you tie to them in modelence.config.json, which are usually DATABASE_URL, the name a new Prisma schema already reads, and REDIS_URL.

Read these variables only in server code, such as route handlers, server actions and server components. Next.js inlines only variables that start with NEXT_PUBLIC_ into the browser bundle, so a connection string without that prefix stays on the server. Pass it to the client your app already uses, such as Prisma, Drizzle, pg, the MongoDB driver or a Redis client.

If the app already uses Supabase or Neon, keep the Prisma or Drizzle setup as it is and declare the URL in env with "type": "secret".

Apply schema changes with prisma migrate deploy or drizzle-kit migrate against the environment's database before deploying, rather than in next start's command. Each container runs the start command, and a rollout starts several, so the migration would run more than once at the same time. prisma generate belongs in the build command, where that isn't a problem.

To add Postgres or Redis, declare them as resources in modelence.config.json beside the Next.js service. The deploy creates them before the new version of the site starts:

modelence.config.json
{
  "resources": {
    // "web": the app's service, as in the next step
    "main-db": { "type": "postgres", "plan": "postgres-1gb" },
    "cache": { "type": "redis" }
  },
  "env": {
    "DATABASE_URL": { "type": "resource", "resource": "main-db", "output": "url" },
    "REDIS_URL": { "type": "resource", "resource": "cache", "output": "url" }
  }
}

Describe the app in modelence.config.json

Modelence builds a Next.js app as a Node.js service. It installs the dependencies and runs next build on an official Node.js image, then starts the app with next start, the production server that ships with Next.js. Add modelence.config.json at the root of the repository and commit it with the app:

modelence.config.json
{
  "$schema": "https://cloud.modelence.com/schema/modelence.config.json?version=1",
  "resources": {
    "web": {
      "type": "service",
      "image": "node-22-slim",
      "build": { "commands": ["npm ci", "npm run build"] },
      "start": { "commands": ["npm start"] }
    },
    "main-db": { "type": "postgres", "plan": "postgres-1gb" }
  },
  "env": {
    "DATABASE_URL": { "type": "resource", "resource": "main-db", "output": "url" },
    "NEXT_PUBLIC_APP_NAME": { "value": "My app", "scopes": ["build", "runtime"] },
    "STRIPE_SECRET_KEY": { "type": "secret" }
  }
}

Adjust the config for your project

  • npm ci installs from package-lock.json. With pnpm, use corepack enable && pnpm install --frozen-lockfile, and with Yarn 2 or newer, corepack enable && yarn install --immutable.
  • next start listens on the PORT variable Modelence sets and on all interfaces, so it needs no flags. Don't use next dev as the start command.
  • Pick image to match engines.node in package.json if it sets one. node-22-slim is the default.
  • next build inlines NEXT_PUBLIC_ variables into the browser bundle, so they need "scopes": ["build", "runtime"]. Never put a secret in one.
  • Declare keys and tokens with "type": "secret" and no value. The deploy asks you for them in the browser, and you can change them later in the dashboard under Environment variables.
  • If the app lives in a subdirectory of a monorepo, set root on the service.
  • With output: 'export' in next.config, there is no server to run. Leave out start and serve the export with "static": [{ "path": "/", "dir": "out" }]. A static export doesn't support cacheComponents or partialPrefetching, which a new app turns on, so remove them from next.config.

Check the build locally

Before the first deploy, run the same commands Modelence will run, in the same order, and check that the app answers on http://localhost:3000. A deploy runs the same build, so anything that fails here fails there too.

bash
npm ci && npm run build && PORT=3000 npm start

Deploy

Run the deploy command in the project directory:

npx modelence@latest deploy

It opens the browser, where you sign in, pick the organization, app and environment (or create them) and enter any required variables the environment doesn't have yet, such as STRIPE_SECRET_KEY above. Then it uploads the project and follows the build until the app is live. You pick the AWS region the first time you deploy an environment, and its containers and databases are created there. Next time, pass --app <app> --env <env> to preselect the target.

The first deploy creates the databases in resources, starts the app once they're ready and attaches the environment's URL, <app>-<environment>.cloud.modelence.app, to the web service. To use your own domain, add it in the dashboard under Domains. Every app has a sandbox and a production environment, and the same file deploys to both.

Let your coding agent do it

Paste Deploy using https://modelence.com/skill.md into Claude Code, Cursor or Codex in your project. The agent reads the docs, writes modelence.config.json for your project, checks the build and runs the deploy, and it stops to ask you whenever a step needs your account.

Production readiness

Add a health check

By default Modelence counts a container as healthy once it accepts connections on its port. An HTTP check is stricter: the container has to answer a path with a 2xx status within 4 seconds, so a new version that starts but can't serve pages never takes traffic. Add a route handler:

// app/api/health/route.ts
import { connection } from 'next/server';

export async function GET() {
  await connection();
  return Response.json({ status: 'ok' });
}

await connection() makes Next.js run the handler on every request instead of prerendering it at build time. Unlike export const dynamic = 'force-dynamic', it also builds with Cache Components, which new apps turn on. Then point the service's check at the route in modelence.config.json:

"healthCheck": { "type": "http", "path": "/api/health" }

Redirects are followed, so if your proxy (middleware before Next.js 16) redirects signed-out visitors, leave /api/health out of its matcher.

Keep state out of the server

Each container has its own disk, which starts empty on every deploy. That includes the Next.js cache in .next/cache, so pages rendered with incremental static regeneration and optimized images are built again after a deploy, and with several containers, revalidating a page on one container doesn't update the others. Prefer time-based revalidation, or set a custom cacheHandler in next.config.js that keeps the cache in Redis if pages must update everywhere at once. Store user uploads in object storage and sessions in the database or Redis.

Under Configure in the dashboard, set the service to two or more containers so the site stays up if one crashes or a zone goes down. Availability covers how deploys and rollbacks keep it serving.

Keep server actions working across deploys

Next.js generates a new encryption key on every clean build, and Server Action IDs change with it. During a rollout the old and new versions serve side by side for a short time, and a tab still open on the old version gets "Failed to find Server Action" when it submits a form. Set the key yourself so actions you haven't changed keep their IDs. Generate one with openssl rand -base64 32, declare it as a secret that also reaches the build, and enter the value when the deploy asks for it:

"NEXT_SERVER_ACTIONS_ENCRYPTION_KEY": { "type": "secret", "scopes": ["build", "runtime"] }

Next.js keeps the key in the server build output, and it never reaches the browser bundle.

Run background jobs as their own service

Don't run a queue consumer or scheduled job inside the Next.js server. With several containers, each one would run every job, and emails or charges would go out twice. Add a second service to modelence.config.json with its own start command, "healthCheck": { "type": "none" } and "rollout": "recreate", as described in Multiple services.

Watch logs and metrics

Logs need no setup. Everything the app writes to its output shows on the environment's Logs page. For traces and metrics, add a telemetry resource and tie the standard OpenTelemetry variables to it, as described in the modelence.config.json reference.

Modelence vs Vercel, Railway and Render for Next.js

People choosing a host for Next.js usually weigh Vercel, the company behind Next.js, against general app platforms such as Railway and Render. The table compares how each one runs a Next.js app, based on each platform's own docs and pricing pages in November 2026.

PlatformHow Next.js runsDatabasesRegionsPricing modelBest for
Modelencenext start as long-running containers on AWS, across two availability zonesManaged MongoDB, Postgres and Redis in the app's region17 AWS regionsFree plan; paid plans from $20 a month with $20 of usage included and no per-seat feeFull-stack apps that should run with their database and backups in one AWS region
VercelPages from a global CDN, and server code as Vercel Functions in one region (iad1) by defaultPostgres, Redis and others through Marketplace providers such as Neon, Upstash and SupabaseGlobal CDN; functions in 1 region on Hobby and up to 5 on ProHobby is free for personal, non-commercial use; Pro is $20 per developer seat a month with $20 of credit, then meteredSites built on ISR and the CDN, and teams that review each pull request on a preview URL
RailwayA long-running server built with Railpack; its Next.js guide uses standalone outputPostgres, MySQL, Redis and MongoDB from templates4 (California, Virginia, Amsterdam, Singapore)Hobby $5 or Pro $20 a month, each including that much usage; CPU and memory billed per secondProjects that want the app and its databases on one canvas with usage-based billing
RenderA Node.js web service with your build and start commands, or a static site for an exportManaged Postgres and Key Value (Redis-compatible)5 (Oregon, Ohio, Virginia, Frankfurt, Singapore)Hobby $0 or Pro $25 a month per workspace, plus instances from $7 a month; free instances spin down after 15 idle minutesTeams that want fixed instance prices and, on Pro, autoscaling

Vercel is the stronger choice for a site that is mostly pages served from its CDN with ISR, or for a team that reviews every pull request on a preview URL, as long as the database can live with another provider. Railway and Render run Next.js as an ordinary server the way Modelence does, and Render's fixed instance prices make the monthly bill easy to predict. Modelence fits an app whose server does real work, such as server actions against a database, and that should run with its database and backups in one AWS region you choose. Modelence vs Vercel and Railway vs Render go into more detail, and Move from Railway covers a migration.

Next steps

Frequently asked questions

Can I deploy Next.js without Vercel?

Yes. Next.js runs on any host that runs Node.js, and with next build and next start every feature works, including server components, route handlers, server actions, streaming, image optimization and proxy (called middleware before Next.js 16). Modelence runs that same next start server in containers on AWS, so the app behaves the way its production build does on your machine.

Can I host a Next.js app on AWS without setting up AWS myself?

Yes. Modelence runs on AWS and sets up the containers, databases and backups for you, so you never open the AWS console. You pick one of 17 AWS regions on the first deploy, and the Next.js containers, spread across two availability zones, and the app's databases are created there.

Do I need output: 'standalone' to deploy Next.js?

No. Modelence installs the project's full dependencies and runs next start, so the default build output works as it is. Standalone output makes a smaller self-contained folder, which helps when you build your own Docker image. If you use it, copy public and .next/static into .next/standalone after next build and start the app with node .next/standalone/server.js, since next start does not work with standalone output.

Why are my NEXT_PUBLIC_ variables undefined in production?

Because next build inlines NEXT_PUBLIC_ values into the browser bundle, so they have to exist when the app is built, not only when it starts. On Modelence, declare them in modelence.config.json with "scopes": ["build", "runtime"], and deploy again after changing one. Variables without the prefix are read on the server at runtime and only need the default scope.

Does ISR work when Next.js runs in more than one container?

Yes, with one caveat: each container keeps its own Next.js cache on its own disk, and the pages it regenerates are gone when the next deploy replaces it. Time-based revalidation works without changes, but revalidatePath and revalidateTag only clear the cache in the container that handles the call. If you revalidate on demand, set a cacheHandler in next.config that keeps the cache in Redis, which Modelence can run in the same region as the app.

Why do I get "Failed to find Server Action" after a deploy?

Next.js generates a new encryption key on every clean build, and Server Action IDs change with it, so a page still open from the previous version calls IDs the new server doesn't know. Generate a key with openssl rand -base64 32 and declare NEXT_SERVER_ACTIONS_ENCRYPTION_KEY in modelence.config.json as a secret with "scopes": ["build", "runtime"]. Actions you haven't changed then keep their IDs from one deploy to the next.

Can I deploy a static Next.js export?

Yes. Set output: 'export' in next.config, leave out the start command in modelence.config.json and serve the out directory with a static entry, so no server process runs. A new app also turns on cacheComponents and partialPrefetching, which a static export doesn't support, so remove both from next.config first.

How do I run Prisma or Drizzle migrations?

Run prisma migrate deploy or drizzle-kit migrate against the environment's database before you deploy, not in the start command. Every container runs the start command, and a rollout starts several at once, so a migration there would run in parallel. prisma generate is safe to keep in the build commands.

Can I use my own domain?

Yes. Add it in the dashboard under Domains and attach it to the service that runs Next.js, and Modelence issues its TLS certificate. Paid plans include unlimited custom domains, and the free plan uses a Modelence subdomain.

How much does it cost to host a Next.js app on Modelence?

You can start on the free plan, which runs one instance that sleeps when idle. Paid plans start at $20 a month with that amount included as cloud usage, and containers are billed hourly by size, from about $9 a month for the smallest one with 0.25 vCPU and 0.5 GB of memory.

Deploy other stacks

Deploy your Next.js app

Start on the free plan and move up when your app needs more.

Or paste this into your coding agent