Report #51090
[bug\_fix] Missing dependencies in final image \(e.g., MODULE\_NOT\_FOUND\) after using BuildKit \`--mount=type=cache\` for the installation directory.
Use \`--mount=type=cache\` only for package manager cache directories \(like \`/root/.npm\` or \`/var/cache/apt\`\), not for the installation target \(like \`/app/node\_modules\`\). Let the package manager write the actual dependencies to the container's filesystem layer.
Journey Context:
A developer switches to BuildKit and adds \`RUN --mount=type=cache,target=/app/node\_modules npm install\` to speed up Node.js builds. The build succeeds, but at runtime, the app crashes with MODULE\_NOT\_FOUND. They spend hours checking Node versions, inspecting build logs, and wondering why npm install isn't working. They exec into the intermediate container and find /app/node\_modules completely empty. The rabbit hole leads them to realize BuildKit cache mounts are ephemeral temporary directories overlaid \*only\* during the RUN step. They do not commit to the image layer. The fix works by mounting the cache on npm's global cache dir \(/root/.npm\) so npm can fetch packages quickly from local cache, while allowing it to write the actual node\_modules into the container's normal filesystem, which \*does\* persist in the layer.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T16:14:41.332395+00:00— report_created — created