Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

Could not find a production build in the '.next' directory: how to fix it in Next.js

Could not find a production build in the '.next' directory means next start found no BUILD_ID file in the build folder: next build did not run, wrote its output somewhere else (distDir, another working directory in a monorepo), or the folder was lost between build and start, as in a Docker stage that does not copy it. Run next build before next start in the same folder. A static export (output: 'export') has no server to start: serve the out folder instead.

By Livops team, the people who build and run LivopsUpdated

What next start prints when there is no build to start[2]:

Error: Could not find a production build in the '.next' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id

next start, or a custom server in production mode, needs a production build[1], and it did not find one. This page explains what it looks for, lists the five usual reasons it is missing on a server, and shows how to check before you deploy. For the whole setup of a Next.js app in production, see Next.js hosting.

What next start looks for

When the production server boots, it reads a file named BUILD_ID from the build folder[2][3]. If that file does not exist, it throws this error, with the folder it looked in between the quotes[2]. Next.js’s own fixes are two lines: run next build to create a production build before booting the production server, or run next dev if you meant the development server[1].

The CLI says the same: next start starts the application in production mode, and the application should be compiled with next build first[4]. The usual package.json[5]:

{
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start"
  }
}

Then npm run build builds the app and npm run start starts the Node.js server[5]. Both, in that order, in the same folder.

Causes, most common first

1. next start ran without next build

The deploy runs only npm start, the build step is missing or was skipped, or the build failed and the start ran anyway. Add next build as the build step, and make a failed build stop the deploy.

A .next folder from next dev does not count: development builds output to .next/dev instead of .next[4].

2. The build went to another folder: distDir

distDir in next.config.js replaces .next with a folder of your choice; with distDir: 'build', next build writes to build[6]. next start reads the same config, and the error names the folder it actually searched[2]. If the name between the quotes is not where your build went, the build and the start are not reading the same next.config.js, or one of them ran in another folder (cause 3).

distDir is resolved relative to the folder that holds next.config.js[6]. Next.js takes ownership of that folder and will sometimes delete its contents[6], so do not point it at a folder that holds anything else.

3. The wrong working directory, often in a monorepo

next build and next start both take an optional directory and use the current directory when none is given[4]. In a monorepo, a build run inside apps/web and a start run from the repository root look at two different folders. Run both in the app’s folder, or pass it to both:

next build apps/web
next start apps/web

4. The build folder was lost between build and start

The build succeeded, but the start runs somewhere that does not have its output: a Docker stage that copies only the source and node_modules, a .dockerignore that leaves .next out, or a CI job that builds in one job and starts in another without passing the folder along.

Next.js’s own Docker example does not run next start at all. It builds with output: 'standalone', copies .next/standalone, .next/static and public from the build stage into the final stage, and starts node server.js[8]:

COPY --from=builder --chown=node:node /app/public ./public
COPY --from=builder --chown=node:node /app/.next/standalone ./
COPY --from=builder --chown=node:node /app/.next/static ./.next/static

CMD ["node", "server.js"]

output: 'standalone' creates .next/standalone, which can be deployed on its own without installing node_modules, together with a minimal server.js to use instead of next start[7]. That server does not copy public or .next/static by default; copy them in, and it serves them[7]:

cp -r public .next/standalone/ && cp -r .next/static .next/standalone/.next/
node .next/standalone/server.js

next start with standalone output only warns and points you to node .next/standalone/server.js[10]. If you keep next start instead, copy the whole build folder to the final stage, along with node_modules, package.json and public.

5. A static export: there is no server to start

With output: 'export' in next.config.js, next build writes the app as HTML, CSS and JS into an out folder[9], and the result can be hosted on any web server that serves static files[9]. There is nothing for next start to run, and it refuses with a different message[10]:

"next start" does not work with "output: export" configuration. Use "npx serve@latest out" instead.

Serve the out folder as a static site. If you need features that require a server, such as cookies, rewrites, redirects, Server Actions or Image Optimization with the default loader, a static export does not support them[9]: remove output: 'export' and deploy the app as a Node.js server with next build and next start.

How to confirm the fix locally

Build and start the way the server will, from a clean folder:

rm -rf .next
npm run build
ls .next/BUILD_ID
npm run start

If ls finds .next/BUILD_ID (or BUILD_ID in your distDir), next start has what it needs. For a Docker image that runs next start, run the same ls inside the final image: docker run --rm --entrypoint ls your-image .next. next start listens on port 3000 by default, or on the PORT variable[4].

On Livops

  • Next.js is detected. Livops reads your repository, detects Next.js and fills in the install, build and start commands. You can change them in the cabinet or in a livops.json file.
  • The build runs on every deploy. Every git push to the connected branch starts a new deploy, and Livops builds the app before it starts it. If the build fails, the log shows why and the old version keeps running.
  • Do not build in the start command. While an app runs, its code is read-only, so a start command that runs next build cannot write .next. Keep the build in the build step. A .next folder you upload is not used either: uploading a folder drops .next, node_modules and the like.
  • The start command is set once. In livops.json, build is applied on every deploy, but start only when the project is created. To change the start command later, use Settings → Start; see No start command.
  • Server or static. A Next.js app that renders pages on the server is deployed as an Application, not a static site; deployed as a site, it ends with ssr_output, and the fix is to make it an Application. A static export is a static site, which is free on Livops. If the build output folder is not found (no_output), set it in Settings → Output. See No site found.
  • Monorepos and Dockerfiles. For a monorepo, give the folder the project lives in (for example apps/web) when you connect the repository. If the repository has a Dockerfile, it is used, and what its final stage copies is up to you (cause 4).
  • Listen on PORT. An Application must listen on the port in the PORT variable; next start reads it.

If the build itself fails, see JavaScript heap out of memory in next build or Module not found: Can’t resolve. If it starts but nothing answers, see App not listening on the port.

Questions

What exactly is next start looking for?

A file named BUILD_ID inside the build folder, which is .next unless distDir says otherwise. next build writes it. If the file is not there, next start stops with this error and names the folder it looked in.

I ran next dev. Why is there still no production build?

next dev is the development server, not a build. Development output even goes to .next/dev rather than .next, so it leaves no production build behind. Run next build, then next start.

Can I commit the .next folder instead of building on the server?

It is the output of next build, with build-time values such as NEXT_PUBLIC_ variables already inlined, so build where you deploy instead. Some deploy tools also drop .next from what you upload: Livops does when you upload a folder.

My build runs in a different folder than my start. What do I do?

Both next build and next start take a directory argument and use the current directory when it is missing. Run them in the same folder, the one with next.config.js, or pass the same folder to both, for example next build apps/web and next start apps/web.

Do I use next start with output standalone?

No. With output standalone, next start only warns and tells you to run node .next/standalone/server.js. That minimal server does not copy the public and .next/static folders by default, so copy them next to it.

My project uses output export. How do I start it?

You do not. A static export has no Next.js server: next build writes HTML, CSS and JS into the out folder, and next start refuses to run with output export. Serve the out folder with any static web server, or deploy it as a static site.

Sources

  1. Next.js Docs — Could not find a production build (nextjs.org). Checked .
  2. Next.js source — packages/next/src/server/next-server.ts (raw.githubusercontent.com). Checked .
  3. Next.js source — packages/next/src/shared/lib/constants.ts (raw.githubusercontent.com). Checked .
  4. Next.js Docs — next CLI (nextjs.org). Checked .
  5. Next.js Docs — Deploying (nextjs.org). Checked .
  6. Next.js Docs — distDir (nextjs.org). Checked .
  7. Next.js Docs — output (nextjs.org). Checked .
  8. Next.js example — examples/with-docker/Dockerfile (raw.githubusercontent.com). Checked .
  9. Next.js Docs — Static Exports (nextjs.org). Checked .
  10. Next.js source — packages/next/src/server/next.ts (raw.githubusercontent.com). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your Next.js app

Got a question? — @livops_uz