Card 03/ 07

GotchaDifficulty: Advanced1 min

Why One Line of Source Reinstalls Every Dependency

You fix a typo in one file and the build spends ten seconds installing dependencies that did not change.

Dockerfiletext
FROM node:20-alpine
WORKDIR /app
COPY . .
RUN npm ci
CMD ["node", "server.js"]

COPY . . copies your whole project, so its layer changes whenever any file does. The cache is a chain: once a layer differs, every instruction after it runs again, whether or not that instruction cares about what changed.

RUN npm ci sits after the copy, so a one-character edit to a source file invalidates the install of every dependency. The install has no idea anything is different; it just no longer has a cached layer to sit on.

The order is the bug. Nothing here is wrong except which line came first.