Deploy a NestJS app
nest build and the compiled app on $PORT, a smaller deploy without dev dependencies, secrets and PostgreSQL.
On this page
Gregale deploys a NestJS app from its source: it installs dependencies with
the package manager your lockfile names, runs the build script, and starts
the compiled app. What every framework shares is on
Deploy a web framework; what NestJS shares with Express,
Fastify and Hono is on Deploy an Express app.
Prepare the app
Listen on the port Gregale gives you, on every interface, in src/main.ts:
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableShutdownHooks();
await app.listen(process.env.PORT ?? 3000, "0.0.0.0");
}
bootstrap();
app.listen(port) without a host also listens on every interface. Passing
"localhost" or "127.0.0.1" makes the app unreachable.
enableShutdownHooks() runs your providers' onModuleDestroy when Gregale
stops an instance, so a database pool closes cleanly.
Add a cheap health route that doesn't touch the database:
import { Controller, Get } from "@nestjs/common";
@Controller()
export class HealthController {
@Get("healthz")
health() {
return { ok: true };
}
}
With a health path set, a deployment goes live only once it answers 2xx.
Build and start scripts
Gregale runs npm run build when the script exists, then starts the app with
npm run start:prod if there is one, otherwise npm run start. A new Nest
CLI project already has both:
{
"scripts": {
"build": "nest build",
"start": "nest start",
"start:prod": "node dist/main"
}
}
start:prod runs the compiled output, so that's what Gregale starts. Don't
point it at nest start --watch or ts-node: a development server gets a
development_start_command warning and starts slowly on every cold boot.
Set the start command and health path in gregale.yaml to skip inference:
hosting:
start: node dist/main.js
health: /healthz
NestJS 12 is ESM-only. A project on it sets "type": "module" in
package.json, compiles with module: nodenext, and imports local files
with a .js extension. tsc with experimentalDecorators and
emitDecoratorMetadata builds it without the Nest CLI.
Keep the deployed app small
The build needs TypeScript and the Nest CLI; the running app doesn't. When
the deploy ships node_modules, as it does when a release command runs a
script from it, drop the development dependencies at the end of the build:
{
"scripts": {
"build": "nest build && npm prune --omit=dev"
}
}
Run that build only on Gregale, or it removes your local tools too; the
example repository below skips the prune when a .git directory exists. The
app layer is limited to 256 MB on Free and 512 MB on Hobby; see
Plans.
Deploy
From the project root, preview what Gregale detects, then deploy:
gregale deploy --dry-run
gregale deploy --name my-nest-api
If it fails:
gregale app health my-nest-api --errors
gregale logs my-nest-api
Configuration and secrets
gregale env set --app my-nest-api JWT_SECRET="$JWT_SECRET"
Values arrive in process.env, where @nestjs/config's ConfigModule reads
them. See Secrets.
Add PostgreSQL
Declare the database in gregale.yaml, and the first deploy creates it:
release:
command: node scripts/migrate.js
databases:
- database: my-nest-db
env: DATABASE_URL
access: read_write
create:
postgres_major: 17
- database: my-nest-db
env: MIGRATION_DATABASE_URL
access: migration
The serving app gets DATABASE_URL, which reaches the database through a
connection pooler in transaction mode, so session state doesn't carry
between transactions. node-postgres (pg) sends unnamed statements, which
work there as they are; with another client, turn off named prepared
statements. Set the pool's idleTimeoutMillis so idle connections close and
the database can suspend:
import pg from "pg";
const pool = new pg.Pool({
connectionString: process.env.DATABASE_URL,
max: 5,
idleTimeoutMillis: 30_000,
});
Run migrations from the release command, not when the app starts.
Only the release command receives MIGRATION_DATABASE_URL, a direct
connection that may change the schema; a failed migration keeps the previous
version serving. See Managed PostgreSQL.
Example repository
gregale-dev/nestjs-postgres
is a NestJS 12 notes API on this page's setup: a module with a controller,
a service and validated DTOs, node-postgres, SQL migrations as the release
command, and end-to-end tests with Node's test runner. Its gregale.yaml
creates the database, so one command deploys both:
git clone https://github.com/gregale-dev/nestjs-postgres
cd nestjs-postgres
gregale deploy --name my-nest-api
In our test it deployed in 81 seconds with a 92 MB app layer, and a parked app was restored from its snapshot in 66 to 74 ms.
Related articles
Deploy NestJS 12 with PostgreSQL covers ESM-only NestJS 12, start:prod, node-postgres behind a pooler and migrations before traffic.
Spotted a problem on this page? Tell us