Docker container for Next.js fails with Cannot find module '/app/server.js'
Error: 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.
Fix it fast
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/standalone2Match what you see, then jump to its fix
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.jsConfirmed by nextjs.org
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 /app2Fix
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 /appConfirmed by nextjs.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Servers
The Next.js container starts with docker run but Compose fails with next: not found
/bin/sh: 1: next: not found
The shell cannot find the next binary.
3 causes, each with a fixServers
ng serve runs in the container but localhost:4200 does not open
The browser cannot open localhost:4200 although ng serve is running in the container
ng serve listens on localhost by default, which inside a container is the container itself.
3 causes, each with a fixServers
bcrypt_lib.node: invalid ELF header inside a Docker container
Error: /app/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node: invalid ELF header
bcrypt is a compiled addon, so the file in node_modules only runs on the operating system it was installed on.
3 causes, each with a fixServers
cdk deploy fails in a GitLab job with spawnSync docker ENOENT
code: 'ENOENT'
ENOENT means the operating system found no docker program to run.
3 causes, each with a fixServers
Cypress says the npm package is installed but the binary is missing
Cypress binary is missing after npm install in CI or in a Docker build
The npm package is only a launcher.
3 causes, each with a fixServers
Node.js app says Cannot find module only when it runs under Docker Compose
The same image starts fine with docker run but fails with docker compose up
The image installed node_modules, but a Compose volume mounted over the app folder hides them.
3 causes, each with a fix