# Next.js

Next.js is a full-stack React framework. This guide covers running Next.js with Specific.

## Configuration

For smaller Docker images and faster cold starts, enable standalone output in `next.config.js`:

```javascript
module.exports = {
  output: "standalone",
};
```

Then reference the standalone output to run the server:

```hcl
build "web" {
  base = "node"
  command = "npm run build"
}

service "web" {
  build = build.web
  command = "node .next/standalone/server.js"

  endpoint {
    public = true
  }

  env = {
    PORT = port
  }

  dev {
    command = "npm run dev"
  }
}
```

Next.js reads `PORT` from the environment automatically, so no additional configuration is needed.

## Important note on pre-rendering during builds

During a build, Next.js will load a lot of code to perform pre-rendering. That code can not reference any environments variables from `specific.hcl`, like database URLs or API keys from secrets, as those are not available during the build and will cause it to fail. Ensure all code that relies on these env vars DOES NOT get executed during the build phase.
