npm "EROFS: read-only file system" - Fix Writes to a Read-Only Path in CI
EROFS means npm attempted to write to a path mounted read-only - a cache dir, node_modules, or a global prefix on a read-only layer. The fix is to redirect npm’s writes to a writable location.
What this error means
npm fails with EROFS: read-only file system for a path under the cache, prefix, or install dir. It is common in hardened containers, distroless/read-only root filesystems, or when installing into a read-only mounted volume.
npm error code EROFS
npm error syscall mkdir
npm error path /nonexistent/.npm
npm error errno -30
npm error Error: EROFS: read-only file system, mkdir '/usr/lib/node_modules/...'Common causes
The npm cache or HOME points at a read-only path
A container with a read-only root filesystem leaves $HOME/~/.npm unwritable, so npm cannot create its cache directory.
Installing into a read-only mount
A global prefix or a project dir on a read-only volume cannot be written, so install or link operations hit EROFS.
How to fix it
Redirect npm writes to a writable dir
Set the cache and prefix to a writable, ephemeral location.
export npm_config_cache=/tmp/.npm
export npm_config_prefix=/tmp/.npm-global
npm ciMount a writable working area
- Give the container a writable
/tmpor a mounted volume for the workspace and cache. - Ensure
$HOMEis writable, or overridenpm_config_cache. - Install on a writable layer, not a read-only mount.
How to prevent it
- Point npm cache/prefix at a writable path on read-only filesystems.
- Install on writable layers, not read-only mounts.
- Provide a writable HOME/tmp in hardened containers.