From 0a31f578758786d5af1262b9ecff7eb1d858a62f Mon Sep 17 00:00:00 2001 From: Gerald Sim Date: Sat, 2 May 2026 13:54:05 +0930 Subject: [PATCH] fix(backend): build stage installs devDependencies for `nest build` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The build stage failed two ways from a clean checkout: 1. `ENV NODE_ENV production` at the top — historically (npm <8) made `npm ci` skip devDependencies. Modern npm ignores NODE_ENV for ci defaults, but the line is misleading. 2. `npm ci` ran with only the workspace root package.json on disk (apps/backend/package.json wasn't copied yet), so npm skipped the apps/backend workspace entirely. devDependencies were never installed and `nest build` failed with `sh: nest: not found`. Fix: - Set `ENV NODE_ENV development` in the build stage so the intent is explicit (and any future tooling that does honour NODE_ENV behaves). - Copy `apps/backend/package.json` before `npm ci`, mirroring the per-workspace manifest pattern used in other multi-workspace Dockerfiles. npm now resolves the workspace and installs all deps including the NestJS CLI. The final production runtime stage (further down in the same Dockerfile) keeps `NODE_ENV=production` and only copies `dist` + production `node_modules`, so the shipping image is unchanged. Verified: docker build -f apps/backend/Dockerfile --target=production . → completes successfully --- apps/backend/Dockerfile | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/apps/backend/Dockerfile b/apps/backend/Dockerfile index ce275db54..484b4bc6b 100644 --- a/apps/backend/Dockerfile +++ b/apps/backend/Dockerfile @@ -37,17 +37,26 @@ FROM node:20.11.1-alpine3.19 as build WORKDIR /app # RUN apk add --no-cache libc6-compat -# Set to production environment -ENV NODE_ENV production +# Build stage runs as development so `npm ci` installs devDependencies +# (NestJS CLI lives in apps/backend's devDependencies; without it +# `nest build` fails with `sh: nest: not found`). The final production +# stage below copies dist + production-only node_modules — devDeps +# don't ship in the runtime image. +ENV NODE_ENV development # Re-create non-root user for Docker # RUN addgroup --system --gid 1001 node # RUN adduser --system --uid 1001 node -# Copy workspace root files +# Copy workspace root manifests + the backend workspace's package.json so +# `npm ci` resolves the backend workspace and installs its devDependencies. +# Without apps/backend/package.json on disk before `npm ci`, npm skips +# the workspace's deps entirely and `nest build` fails even though +# devDeps are nominally enabled. COPY --chown=node:node package*.json ./ +COPY --chown=node:node apps/backend/package.json ./apps/backend/ -# Install all workspace dependencies +# Install all workspace dependencies (incl. devDeps — needed for `nest build`) RUN npm ci # Copy backend source code