Skip to content

Commit 2112311

Browse files
committed
Fix Dockerfile for hoisted npm workspace dependencies
- Copy root node_modules instead of workspace-specific ones - Add separate prod-deps stage for production dependencies only - Set NODE_PATH to resolve modules from root in production - Fixes 'node_modules not found' error in build
1 parent 2477967 commit 2112311

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

Dockerfile

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ FROM node:20-alpine AS frontend-build
1616

1717
WORKDIR /app
1818

19-
# Copy dependencies from deps stage
19+
# Copy root node_modules (npm workspaces hoists dependencies)
2020
COPY --from=deps /app/node_modules ./node_modules
21-
COPY --from=deps /app/frontend/node_modules ./frontend/node_modules
2221

23-
# Copy frontend source
22+
# Copy workspace packages and frontend source
23+
COPY --from=deps /app/package*.json ./
2424
COPY frontend/ ./frontend/
25-
COPY package*.json ./
2625

2726
# Build frontend
2827
RUN npm run build:frontend
@@ -32,28 +31,42 @@ FROM node:20-alpine AS backend-build
3231

3332
WORKDIR /app
3433

35-
# Copy dependencies from deps stage
34+
# Copy root node_modules (npm workspaces hoists dependencies)
3635
COPY --from=deps /app/node_modules ./node_modules
37-
COPY --from=deps /app/backend/node_modules ./backend/node_modules
3836

39-
# Copy backend source
37+
# Copy workspace packages and backend source
38+
COPY --from=deps /app/package*.json ./
4039
COPY backend/ ./backend/
41-
COPY package*.json ./
4240

4341
# Build backend
4442
RUN npm run build:backend
4543

46-
# Stage 4: Production
44+
# Stage 4: Production - Install only production dependencies
45+
FROM node:20-alpine AS prod-deps
46+
47+
WORKDIR /app
48+
49+
# Copy workspace package files
50+
COPY package*.json ./
51+
COPY backend/package*.json ./backend/
52+
COPY frontend/package*.json ./frontend/
53+
54+
# Install only production dependencies
55+
RUN npm ci --omit=dev
56+
57+
# Stage 5: Final production image
4758
FROM node:20-alpine
4859

4960
WORKDIR /app
5061

5162
# Install runtime dependencies
5263
RUN apk add --no-cache nginx supervisor
5364

65+
# Copy production node_modules
66+
COPY --from=prod-deps /app/node_modules ./node_modules
67+
5468
# Copy built backend
5569
COPY --from=backend-build /app/backend/dist ./backend/dist
56-
COPY --from=backend-build /app/backend/node_modules ./backend/node_modules
5770
COPY --from=backend-build /app/backend/package.json ./backend/
5871

5972
# Copy built frontend
@@ -92,7 +105,7 @@ stdout_logfile_maxbytes=0
92105
stderr_logfile=/dev/stderr
93106
stderr_logfile_maxbytes=0
94107
directory=/app/backend
95-
environment=NODE_ENV=production
108+
environment=NODE_ENV=production,NODE_PATH=/app/node_modules
96109
EOF
97110

98111
EXPOSE 80

0 commit comments

Comments
 (0)