Skip to content

fix: use a user-writable npm cache location - #2702

Closed
Cdineshreddy12 wants to merge 2 commits into
nodejs:mainfrom
Cdineshreddy12:fix/npm-cache-non-root
Closed

Cdineshreddy12 wants to merge 2 commits into
nodejs:mainfrom
Cdineshreddy12:fix/npm-cache-non-root

Conversation

@Cdineshreddy12

@Cdineshreddy12 Cdineshreddy12 commented Nov 21, 2026 •

Copy link
Copy Markdown

Description

Sets NPM_CONFIG_CACHE=/tmp/.npm in the three templates and all generated Dockerfiles, and adds two build-test steps (arbitrary-UID install, read-only root filesystem install) so the behaviour stays covered.

npm defaults its cache to $HOME/.npm. When the image runs as a user with no home directory — an arbitrary --user UID on Jenkins, rootless Docker, GitHub/GitLab runners — HOME resolves to / and every npm install dies with EACCES: mkdir /.npm.

Motivation and Context

Fixes #2495.

The obvious objection is that NPM_CONFIG_CACHE is already the documented workaround, so baking it in looks like giving up on a "real" fix. I went through the alternatives before settling on this; /tmp is the default most likely to work without extra configuration, for reasons worth stating precisely:

  • A dedicated world-writable cache dir baked into the image (e.g. /usr/local/share/npm-cache with mode 1777): on its own, in a single ephemeral container with nothing mounted there, this works fine — only one UID ever touches it. The problem shows up once that directory is persisted and reused across runs under different UIDs, which is a common CI pattern for speeding up repeated installs (a cache action, a cache: key, a shared build-agent host mount). npm's cache (cacache) ties integrity bookkeeping to whichever UID wrote it first, and the next UID hits the same class of EACCES / "cache folder contains root-owned files" error this issue is about — not solved, just relocated to whoever reuses the cache.
  • Any other fixed path can also satisfy readOnlyRootFilesystem: true, since that setting only locks the container's own filesystem — an explicitly mounted volume stays writable at any path. The reason to default to /tmp specifically is convention, not necessity: it's the one path that hardened setups (restricted Kubernetes pod security profiles, common CI runner images) already tend to mount a tmpfs/emptyDir at without the operator adding anything. Any other path needs that same mount, just pointed somewhere nonstandard.
  • A per-UID path (/tmp/.npm-$UID) can't be expressed in a Dockerfile ENV — there's no runtime expansion.
  • npm_config_userconfig on its own doesn't move the cache at all.

So pointing the default cache at /tmp isn't the workaround promoted out of convenience — it's the option that needs the least extra configuration to avoid the scenarios this issue and its persistent-cache cousin both report.

Trade-offs, stated plainly:

  • The cache no longer persists across container restarts. For a base-image default I'd argue that's correct — CI containers are ephemeral, and anyone who wants a persistent cache can still set NPM_CONFIG_CACHE or mount one, exactly as today.
  • The node user previously cached persistently under /home/node/.npm; it now caches under /tmp/.npm. Same override applies if someone depends on the old location.
  • Two different UIDs running npm inside the same container instance would still contend for /tmp/.npm — unavoidable with a fixed ENV, and a much narrower case than the single-UID-per-container CI runs this issue is about.

Documentation: added an "npm cache" section to docs/BestPractices.md covering the default location, the persistence trade-off, and how to override it.

Testing Details

Built 24/bookworm-slim locally and verified:

  • default root usage unchanged (node --version, npm --version, installs fine)
  • npm install succeeds as UID 1002 with no home directory (the Jenkins / rootless case)
  • npm install succeeds with --read-only --tmpfs /tmp as UID 1002 (the readOnlyRootFilesystem case)
  • the node user (UID 1000) works as before, caching under /tmp/.npm

The two new build-test.yml steps run the UID-1002 and read-only-rootfs installs against every image variant in the matrix, so a regression here fails CI.

Example Output

Before (any arbitrary UID):

npm error code EACCES
npm error syscall mkdir
npm error path /.npm

After:

$ docker run --rm --user 1002:1002 -v "$PWD:/app" -w /app node:24-bookworm-slim npm install [email protected]
added 1 package in 1s

Types of changes

  • Documentation
  • Version change (Update, remove or add more Node.js versions)
  • Variant change (Update, remove or add more variants, or versions of variants)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Other (none of the above)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING.md document.
  • All new and existing tests passed.

npm defaults its cache to $HOME/.npm. When the image is run as a user
without a home directory (an arbitrary --user UID on Jenkins, rootless
Docker, or similar CI runners), HOME resolves to / and npm fails with
EACCES trying to mkdir /.npm.

Set NPM_CONFIG_CACHE=/tmp/.npm in the templates and the generated
Dockerfiles so npm install works for any UID, including with a
read-only root filesystem when a writable /tmp is mounted.

Add build-test steps covering an arbitrary-UID npm install and a
read-only root filesystem install.

Fixes nodejs#2495
Explain why the images set NPM_CONFIG_CACHE=/tmp/.npm, the trade-off on
cache persistence, and how to override it.
@ViliusS

ViliusS commented Nov 21, 2026

Copy link
Copy Markdown

To anyone reviewing this - it's very poor workaround, and not real solution. See #2495 (comment) why.

@Cdineshreddy12

Copy link
Copy Markdown
Author

Tightened the description above for precision on two points raised in the linked issue:

  • The cache-ownership concern is specific to a cache directory that's persisted and reused across differently-UID'd runs (a mounted cache volume, common for speeding up repeated installs in CI) — not something that happens to an ephemeral per-container directory on its own.
  • readOnlyRootFilesystem: true only locks the container's own filesystem; a volume can be mounted writable at any path. /tmp is the default that needs no extra configuration under common hardened setups, not the only path that could technically work.

Fix and tests are unchanged — this only corrects the written rationale to be accurate.

@MikeMcC399

Copy link
Copy Markdown
Member

Closing, on the basis that the author of the issue #2495 it states to resolve, rejects the PR.

We can re-open if there is any member of the Node.js Docker team that would sponsor the PR.

@MikeMcC399 MikeMcC399 closed this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Broken NPM cache folder settings do not allow running image without root

4 participants