Heroku

Discover Heroku preset for Nitro!


Preset: heroku (switch to this preset)

Nitro supports deploying on Heroku with minimal configuration.

Using the Heroku CLI

  1. Create a new Heroku app.
    heroku create myapp
  2. Configure Heroku to use the nodejs buildpack.
    heroku buildpacks:set heroku/nodejs
  3. Configure your app.
    heroku config:set NITRO_PRESET=heroku
  4. Ensure you have start and build commands in your package.json file.
    "scripts": {
      "build": "nitro build", // or `nuxt build` if using nuxt
      "start": "node .output/server/index.mjs"
    }

With Nginx

  1. Add the heroku Nginx buildpack here
  2. Change to the 'node' preset in your nuxt.config
    "nitro":{
       "preset":"node",
    }
  3. From the Existing app section of buildpack doc, 2 key steps are required to get things running
    Step 1: Listen on a socket at 'tmp/nginx.socket' Step 2: Create a file '/tmp/app-initialized' when your app is ready to accept connections
  4. Create custom app runner, eg: apprunner.mjs at the root of the project (or any other prefered location), in this file, create a server, using the listener generated by the node preset, then listen on the socket as detailed in the buildpack doc
    import { createServer } from 'node:http'
    import {listener} from './.output/server/index.mjs'
    const server = createServer(listener)
    server.listen('/tmp/nginx.socket') //following the buildpack doc
  5. To create the 'tmp/app-initialized' file, use a nitro plugin, create file 'initServer.ts' at the root of the project (or any other prefered location)
    import fs from "fs"
    export default defineNitroPlugin((nitroApp) => {
       if((process.env.NODE_ENV || 'development') != 'development')fs.openSync('/tmp/app-initialized', 'w')
    })
  6. Finally, create file 'Procfile' at the root of the project, with the Procfile, we tell heroku to start nginx and use the custom apprunner.mjs to start the server
    web: bin/start-nginx node apprunner.mjs
  7. Bonus: create file 'config/nginx.conf.erb' to customize your nginx config. With the node preset, by default, static files handlers will not be generated, you can use nginx to server static files, just add the right location rule to the server block(s), or, force the node preset to generate handlers for the static files by setting serveStatic to true