'Docker Image for NextJs ClientApp failing with an error Cannot find module '/app/server.js'
I am trying to create docker image for my nextjs application. It is working well in my local.
My local node version is 16.14.0 My local nextjs version is 12.x
I have nothing special in my next.config.js except reactStrictMode: true
My Dockerfile is here:
# PART 1 -------------------
FROM node:16-alpine AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json ./
RUN npm install --frozen-lockfile
RUN npm i [email protected]
# PART 2 ----------------
FROM node:16-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# PART 3 ----------------
RUN npm run build
FROM node:16-alpine AS runner
WORKDIR /app
ENV NODE_ENV production
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
USER nextjs
EXPOSE 3000
ENV PORT 3000
CMD ["node", "server.js"]
I have then tried to troubleshoot this by going to interactive mode
docker run -it brij1111-portfolio sh
What I found there is just 3 items.
- next.config.js (file)
- node_modules (folder)
- public (folder)
I did not find .next folder at all. However, in my public folder I can see I have all the images and other items as I have in my local development. So, I feel that .next folder is not there and that could be a reason of I am getting following error.
For me, It looks like the .next folder did not get created at the first place (RUN npm run build) or it is not getting copied over (COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next)
To check this, I have commented on Part 3 and ran Part 1 & 2, and went to the container instance and that was an issue.
I did not find the .next folder even though the RUN npm run build command was executed without any error.
I am not sure what is the issue and How to move forward from here. any suggestion?
Solution 1:[1]
It might that your package.json
file, has the dev: "NODE_ENV=production node server.js"
and you application doesn't have the server.js file as it is not created.. you should look into that.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tyler2P |