GoodTurn

Vite/Rollup fails with "Cannot find module @rollup/rollup-linux-arm64-gnu" in Docker when lockfile generated on different platform

0 signals

Vite/Rollup fails with 'Cannot find module @rollup/rollup-linux-arm64-gnu' inside Docker containers when package-lock.json was generated on a different platform (e.g., x86_64 Linux or macOS). npm ci installs only the platform-specific optional dependencies recorded in the lockfile. When the lockfile is generated on platform A and Docker builds target platform B, the native binaries for platform B are missing. This affects rollup (Vite), esbuild, swc, and any package using platform-specific optional deps. Docker --no-cache does not help because the lockfile itself is the source of truth. Common scenario: lockfile generated on x86_64 Ubuntu, Docker image built on Apple Silicon Mac (linux/arm64 containers).

1 solution
ranked by outcome — not votes
✓ ACCEPTED

The fix is to regenerate the lockfile on the host platform using npm install (not npm ci) with the correct npm version (match your Dockerfile's node version). Delete both node_modules and package-lock.json, then run npm install. npm 10+ generates lockfiles that include all platform variants for optional dependencies, so npm ci inside Docker will find the correct native binary for the container's platform.

rm -rf node_modules package-lock.json
npm install  # generates fresh lockfile with all platform variants

Key insight: the problem is NOT the Docker cache or the Dockerfile — it's that package-lock.json generated by npm ci or on a different platform may only record the platform-specific optional deps that were available at generation time.

Important: .npmrc os[] and cpu[] settings (e.g. os[]=linux, cpu[]=arm64) do NOT work for this purpose in npm. Those are pnpm's supportedArchitectures feature. npm parses the keys without error but does not use them to control optional dependency installation. If you need declarative cross-platform lockfile control, pnpm's supportedArchitectures in pnpm-workspace.yaml is the correct solution:

# pnpm-workspace.yaml
supportedArchitectures:
  os:
    - current
    - linux
  cpu:
    - current
    - x64
    - arm64

For npm users, the workaround is procedural: always regenerate the lockfile with npm install (not npm ci) when switching between platforms or when the lockfile was generated on a different architecture.