Trying to add support for Docker to existing NextJS project but getting "stat app/.next/standalone: file does not exist" error

Issue

I am following this guide to add Docker support to my existing NextJS + TypeScript project and deploy to Google Cloud Run: https://github.com/vercel/next.js/tree/canary/examples/with-docker.

However, the build your container step:

docker build -t nextjs-docker .

keeps failing because it’s giving me an error that "stat app/.next/standalone: file does not exist" error.

I looked into my .next folder and no standalone file is being generated which is why I’m getting that error. How do I get this .next/standalone file to get created?

My next.config.js file looks like this:

module.exports = {
  eslint: {
    ignoreDuringBuilds: true,
  },
  experimental: {
    outputStandalone: true
  }
}

My Dockerfile looks like this:

# Install dependencies only when needed
FROM node:16-alpine AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app

COPY package.json package-lock.json tsconfig.json ./ 
RUN npm ci

# Rebuild the source code only when needed
FROM node:16-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

RUN npm run build

# Production image, copy all the files and run next
FROM node:16-alpine AS runner
WORKDIR /app

ENV NODE_ENV production

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

# You only need to copy next.config.js if you are NOT using the default configuration
COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json

# Automatically leverage output traces to reduce image size 
# https://nextjs.org/docs/advanced-features/output-file-tracing

# Following line is giving me the error
# NOTE: I can not just comment out this line because it will give an error later that "Cannot find module '/app/server.js'"
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV PORT 3000

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

My .next folder looks like:

- .next
  - cache
  - server
  - static
  - traces
  - BUILD_ID
  - build-manifest.json
  - export.marker.json
  - images-manifest.json
  - prerender-manifest.json
  - react-loadasble-manifest.json
  - required-server-files.json
  - routes-manifest.json
  - trace

My understanding was that adding:

experimental: {
  outputStandalone: true
}

to next.config.js file and then running npm run build would create the .next/standalone file but seems to be not working.

Solution

In case this is helpful to anyone else, turns out my version for "next" was set to "^11.1.0" and the standalone folder only works for "next" versions "^12.1.0" and above. Updating my package.json fixed the problem!

Answered By – mj23

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published