Node.js

Run Nitro apps with Node.js runtime.

Preset: node_server

Node.js is the default Nitro output preset for production builds.

Build your project with the Nitro CLI:

nitro build

With the Node server preset, the build output is an entry point that launches a ready-to-run Node server. To try the output locally:

$ node .output/server/index.mjs
Listening on http://localhost:3000

You can now deploy the fully standalone .output directory to the hosting of your choice.

Environment Variables

You can customize server behavior with the following environment variables:

  • NITRO_PORT or PORT (defaults to 3000)
  • NITRO_HOST or HOST
  • NITRO_UNIX_SOCKET - When set to a socket file path, the server listens on the provided UNIX socket.
  • NITRO_SSL_CERT and NITRO_SSL_KEY - When both are set, the server launches in HTTPS mode. This is intended for testing only; in production, run the Nitro server behind a reverse proxy that terminates SSL, such as nginx or Cloudflare.
  • NITRO_SHUTDOWN_DISABLED - Set to 'true' to skip graceful shutdown. Defaults to 'false'.
  • NITRO_SHUTDOWN_SIGNALS - Space-separated list of signals that trigger graceful shutdown. Defaults to 'SIGINT SIGTERM'.
  • NITRO_SHUTDOWN_TIMEOUT - Time (in milliseconds) before a forced shutdown occurs. Defaults to '30000'.
  • NITRO_SHUTDOWN_FORCE - When set to 'true', calls process.exit() at the end of the shutdown process. When 'false', the process simply lets the event loop clear. Defaults to 'true'.

Cluster mode

Preset: node_cluster

To improve performance by leveraging multiple CPU cores, use the cluster preset.

Environment Variables

In addition to the node_server environment variables above:

  • NITRO_CLUSTER_WORKERS: Number of cluster workers (defaults to the number of available CPU cores)

Handler (advanced)

Preset: node_middleware

Nitro also has a lower-level preset that exports a middleware function you can plug into a custom server.

With the Node middleware preset, the build output is an entry point exporting a middleware handler.

Example:

import { createServer } from 'node:http'
import { middleware } from './.output/server'

const server = createServer(middleware)
server.listen(8080)