All posts

Deploying a Next.js 16 + Nx monorepo on Firebase App Hosting

Six undocumented behaviors that cost me hours — silent misconfigurations, misleading error messages, and the one path that finally worked.

Firebase App Hosting and Nx monorepos don't fit neatly together. The platform assumes a single Next.js app at the root; Nx mirrors your entire workspace inside .next/standalone; and when something breaks, the error messages point somewhere else entirely. Here's what I hit and why.

I was running Next.js 16 (App Router), Nx 22, pnpm 10, and Cloudflare in front of Firebase for DNS and Transform Rules.

Don't remove output: 'standalone'

If you're used to trimming next.config.ts down to what feels essential for a given platform, don't do it here.

Firebase App Hosting needs output: 'standalone' to generate a self-contained server.js it can actually run. Without it, the build succeeds and the app fails to start — with errors that don't clearly point back to this as the cause.

ts
// next.config.ts
const nextConfig: NextConfig = {
  output: 'standalone',
  // ...
};

runCommand goes at the top level — not inside runConfig

My apphosting.yaml had this:

yaml
# Wrong
runConfig:
  runCommand: node apps/myapp/.next/standalone/server.js

Firebase silently ignored it. The field is runCommand, and it belongs as a sibling of runConfig, not nested inside it:

yaml
# Correct
scripts:
  buildCommand: pnpm nx build myapp
  runCommand: node apps/myapp/.next/standalone/apps/myapp/server.js
 
runConfig:
  concurrency: 80
  cpu: 1
  memoryMiB: 512

There's no warning when you get this wrong. The command is simply never used and Firebase falls back to a default that doesn't match your project structure.

Paths are relative to the repo root — not rootDirectory

apphosting.yaml has a rootDirectory field that sets where the platform looks for your config file. It's easy to assume that buildCommand and runCommand also execute relative to that directory.

They don't.

Both commands run from /workspace — the root of your repository — regardless of what rootDirectory is set to. In an Nx monorepo, your run command needs the full path from the repo root:

yaml
runCommand: node apps/myapp/.next/standalone/apps/myapp/server.js

rootDirectory is only for YAML discovery. It has no effect on runtime paths.

Nx mirrors your workspace structure inside .next/standalone

In a standard Next.js project, standalone output lives at .next/standalone/server.js. In an Nx monorepo, the entire workspace structure is mirrored:

apps/
  myapp/
    .next/
      standalone/
        apps/
          myapp/
            server.js   <- this is what you need to run

This is correct behavior — Nx preserves node_modules hoisting and workspace paths — but it's not documented anywhere obvious.

PORT timeout errors often mean wrong path, not port issues

When the path to server.js is wrong, Firebase App Hosting reports something like:

"Server did not bind to PORT within the timeout period."

That message implies a networking or concurrency issue. In reality, the process never started because the file doesn't exist at the path you specified. Before debugging port binding, verify that the path in runCommand resolves to an actual file.

Cloudflare: use "DNS only" during domain validation

If you're routing traffic through Cloudflare with Transform Rules, disable the proxy during Firebase's domain validation step. Set the DNS record to gray cloud (DNS only) while Firebase verifies ownership. Once the domain is active and SSL is provisioned, switch back to orange cloud (Proxied) to re-enable your rules. Leaving the proxy on during validation fails silently.

Putting it all together

Here's the apphosting.yaml that finally worked:

yaml
# apphosting.yaml
runConfig:
  concurrency: 80
  cpu: 1
  memoryMiB: 512
  minInstances: 0
  maxInstances: 4
 
scripts:
  buildCommand: pnpm nx build myapp --configuration=production
  runCommand: node apps/myapp/.next/standalone/apps/myapp/server.js

And in next.config.ts:

ts
const nextConfig: NextConfig = {
  output: 'standalone',
};

Nx monorepos are still a rough edge in Firebase App Hosting's documentation. The error messages are misleading, the silent misconfiguration is easy to introduce, and the standalone path structure is a genuine surprise. If you're stuck, log your .next/standalone directory structure after a build and work backward from there.