'Cannot find module '/dist/main' when using docker multi-stage builds
Here is the Dockerfile that I am using to build my nestjs project:
FROM node:14 AS builder
WORKDIR /app
COPY package*.json ./
COPY prisma ./prisma/
COPY protos ./protos/
COPY tsconfig.build.json ./
COPY tsconfig.json ./
RUN npm install
RUN npm run build
COPY . .
FROM node:14-alpine
COPY --from=builder /app/node_modules ./node_modules/
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/dist ./dist/
COPY --from=builder /app/protos ./protos/
COPY --from=builder /app/tsconfig.build.json ./
COPY --from=builder /app/tsconfig.json ./
COPY --from=builder /app/prisma ./prisma/
EXPOSE 5273
CMD ["npm", "run", "start:prod"]
.dockerignore:
.vscode/
node_modules/
npm-debug.log
dist/
graphql/
test/
The container can not work, and I get the :
Cannot find module '/dist/main'
Am I missing something here?
Solution 1:[1]
the main.js file in dist is located on /dist/src/main.js
Solution 2:[2]
Solved: I need to do COPY . . then RUN npm run build.
COPY . .
RUN npm run build
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 | ardritkrasniqi |
| Solution 2 | Erika |
