Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Docker container for Next.js fails with Cannot find module '/app/server.js'

ErrorError: Cannot find module '/app/server.js'

Short answer

server.js only exists when next.config sets output to standalone. The build writes it to .next/standalone, so the image must copy that folder into the runtime stage, and the start command must run from it. Also copy public and .next/static, because standalone does not include them by default.

  • Applies to Next.js 15, Next.js 16, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Build locally and look for server.js in the standalone output. If it is not there, the problem is in the config, not in the Dockerfile.

    npm run build
    ls .next/standalone
  2. 2Match what you see, then jump to its fix

    1. 1.next/standalone does not exist after the buildoutput: 'standalone' is not setFix
    2. 2server.js exists locally in .next/standalone but not inside the imageThe Dockerfile copies the wrong folder to /appFix

What does the error look like?

  • Error: Cannot find module '/app/server.js'
  • code: 'MODULE_NOT_FOUND'

Cause 1 of 2

output: 'standalone' is not set

1Check

The standalone folder, with its minimal server.js, is only created when the option is enabled.

grep -n "output" next.config.*

2Fix

Enable standalone output and rebuild.

// next.config.js
module.exports = {
  output: 'standalone',
}

3Verify

Rebuild and confirm server.js is in the output.

npm run build
ls .next/standalone/server.js

Confirmed by nextjs.org

Did this fix it?

Cause 2 of 2

The Dockerfile copies the wrong folder to /app

1Check

List the files in the final image. If /app holds your whole project or the .next folder instead of the contents of .next/standalone, server.js is not at /app/server.js.

docker run --rm --entrypoint ls my-next-image -la /app

2Fix

In the runtime stage copy the contents of .next/standalone to the working directory, then copy .next/static to .next/static inside it, and public to public. Start with node server.js.

COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
CMD ["node", "server.js"]

3Verify

Rebuild the image and list /app again.

docker build -t my-next-image .
docker run --rm --entrypoint ls my-next-image /app

Confirmed by nextjs.org

Did this fix it?

How do I stop it coming back?

  • Start from the official Next.js with-docker example and change it, not a Dockerfile written from memory.
  • Check ls .next/standalone in CI before the image build.
  • Use a .dockerignore that excludes node_modules and .next from the build context.
  • Pin the Node.js version in the image to a current LTS release.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.