Skip to the solution
MediumSystem Design4 min read

What is the output: 'standalone' build in Next.js and when do you use it?

85 views

The problem

Question presented to candidate: "Our Docker images for a Next.js app are over a gigabyte. What does output: 'standalone' do and how does it change the Dockerfile?"

What a strong answer should cover:

  • output: "standalone" makes next build produce .next/standalone with a minimal server.js and only the files the server needs.
  • It uses output file tracing to copy just the traced dependencies from node_modules, so no npm install is needed in the runtime image.
  • Run it with node server.js; configure PORT and HOSTNAME through environment variables.
  • .next/static and public are not copied by default: copy them next to server.js or serve them from a CDN.
  • Use it for Docker and other self-hosted deployments; managed platforms do not need it.

Clarifying questions expected:

  • "Are static assets served by a CDN?" β€” then you may not need to copy them into the image.
  • "Do any packages load files at runtime that tracing might miss?" β€” they may need outputFileTracingIncludes.

Code / implementation expected: Yes β€” the config and the copy steps.

  • nextjs
  • standalone
  • docker
  • deployment

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

4 min read Β· 8 sections

Read the code

Config, copy steps and start command
# next.config.ts
#   const nextConfig = { output: "standalone" };

npm run build
cp -r .next/static .next/standalone/.next/static
cp -r public .next/standalone/public
cd .next/standalone
PORT=3000 HOSTNAME=0.0.0.0 node server.js

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 93 of 129 in Next.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Next.js questions
Up nextMedium

What are forbidden() and unauthorized() in Next.js?

Continue