Repository navigation
Description
Environment
- Platform: Linux
- Docker Version: Docker Engine v29 (but can be replicated with older versions)
- Node.js Version: LTS 24 and 22 (can be replicated with all versions from v16)
- Image Tag: 22.22.2-alpine
Expected Behavior
Unmodified Node.js Docker image should be runnable without root privileges.
Current Behavior
The image is completely broken as a base build image in environments where one could not modify a running user, e.g., on Jenkins pipelines which by default run under Jenkins user and the UID is most probably not the same as UID 1000 which is used in the image. The same issue can be observed on the free GitHub Actions runners.
It is also broken as a base runtime image on read-only root filesystem environments, e.g., Kubernetes containers with readOnlyRootFilesystem: true.
The source of these issues is that npm_config_prefix is set to /usr/local and npm_config_userconfig is not set at all, so NPM is trying to use HOME folder which in the docker image is set to /. That folder is not writable by any of the users except root.
The only workaround on Jenkins is to set NPM_CONFIG_CACHE to something like /tmp/jenkins/.npm. GitLab/GitHub runners can be fixed in similar manner.
Possible Solution
The real fix should be to create .npm cache folder somewhere writable by any user on the docker image, so at least it works by default on CI pipelines. Maybe create some kind of documented folder specified via npm_config_userconfig so anyone can map it and modify the behaviour of a running user.
Steps to Reproduce
Create a Jenkins pipeline for any Node.js project using NPM and try to build it:
pipeline {
agent {
docker {
image 'node:22.22.2'
}
}
stages {
stage('Build and publish') {
steps {
sh "npm ci"
sh "npm run build"
sh "npm publish"
}
}
}
}
12:51:45 npm error code EACCES
12:51:45 npm error syscall mkdir
12:51:45 npm error path /.npm
12:51:45 npm error errno EACCES
12:51:45 npm error
12:51:45 npm error Your cache folder contains root-owned files, due to a bug in
12:51:45 npm error previous versions of npm which has since been addressed.
12:51:45 npm error
12:51:45 npm error To permanently fix this problem, please run:
12:51:45 npm error sudo chown -R 1002:1002 "/.npm"
Additional Information
I chose Jenkins as an example. The same issue can be reproduced in any CI/CD environment which doesn't run under root user: rootless Docker, GitHub Actions, etc.
As evident from the past, considering cryptic and sometimes incorrect error messages coming from NPM, people constantly struggle to understand why it doesn't work for them in one way or another:
#1734
npm/cli#3910
I know that there are some practices documented how to run this image without a root user, but that's not the point of this ticket. It can be fixed at the source of this image without the need to use all those workarounds.
Activity
MikeMcC399 commentedon Jun 1, 2026
This sounds very similar to cypress-io/cypress-docker-images#914 using a Cypress Docker image in a Jenkins environment.
ViliusS commentedon Jun 1, 2026
@MikeMcC399 while the resulting error is similar, the misconfiguration and the real fix of these images is a little bit different. node.js image here just needs a cache folder setup properly. While cypress needs to have a proper installation directory instead of /root.
MikeMcC399 commentedon Jun 2, 2026
The following example Jenkins pipeline works without modifying the
node:ltsDocker image. InsteadHOME = "${WORKSPACE}"is set as an environment variable.I'm uncertain if there should be any change made to the node Docker images given this result. Also the https://www.jenkins.io/doc/book/pipeline/docker/ documentation shows how a Dockerfile can be used building FROM a
nodeDocker image, so this should allow any other customization to be done.I'm no Jenkins expert though, so this is only a contribution to perhaps resolving this issue. Hopefully there will be experienced Jenkins users who can provide additional comments?
[-]Broken NPM cache folder settings do not allow running image without root[/-][+]Jenkins permissions issue running as non-root with npm[/+]ViliusS commentedon Jun 2, 2026
@MikeMcC399 it's NOT Jenkins permission issue. The old subject was correct. It is the same problem on ANY environment which doesn't run with root privileges: rootless Docker installation, GitHub Actions runners, TeamCity runner, etc.
And setting HOME to WORKSPACE environment variable is not the correct solution. This is highly unrecommended and some would say even dangerous.
[-]Jenkins permissions issue running as non-root with npm[/-][+]Broken NPM cache folder settings do not allow running image without root[/+]Cdineshreddy12 commentedon Nov 20, 2026
I dug into this and can confirm the root cause: the image sets no npm cache configuration at all, so npm falls back to its default of
$HOME/.npm— and when the container runs under an arbitrary--userUID, HOME resolves to/, hence theEACCES: mkdir /.npm. (Small correction to the issue's framing: it's notnpm_config_prefix— that's npm's prefix derived from the node install location, not something the image sets.)I've built and tested a fix locally:
ENV NPM_CONFIG_CACHE=/tmp/.npmin the three templates plus the generated Dockerfiles. Verified against a locally built24/bookworm-slim:node --version,npm --version, installs all fine)npm installsucceeds as UID 1002 (the Jenkins/rootless case from this issue)npm installsucceeds with--read-only --tmpfs /tmpas UID 1002 (thereadOnlyRootFilesystemcase, with the standard emptyDir-at-/tmp mount)nodeuser (UID 1000) works as before, just caching under/tmp/.npmI've also written
build-test.ymlsteps covering the arbitrary-UID and read-only-rootfs scenarios, so this stays covered going forward. Would the team accept a PR along these lines? Happy to adjust the cache location if/tmpraises concerns.ViliusS commentedon Nov 21, 2026
@Cdineshreddy12 you didn't provide any solution. You just repeated what is already said in the description of this issue with the workaround which I have already provided. To really solve this, please first understand the issue yourself and then check how it could be solved.
Cdineshreddy12 commentedon Nov 21, 2026
Fair point that the mechanism is the same as the workaround you documented — the question I'm raising is whether it should become the default. I've opened #2702 making that case: a baked-in shared cache dir hits npm's cross-UID collision problem (cacache can't be safely shared between UIDs), any non-tmpfs path fails your
readOnlyRootFilesystemcase, and a per-UID path can't be expressed in a DockerfileENV— so/tmpis what's left after eliminating the designs that re-break one of your reported scenarios. The alternatives considered, trade-offs, and the CI test matrix are in the PR — happy to be shown a better option over there.ViliusS commentedon Nov 21, 2026
The deliberations you have provided are obvious AI hallucinations. For example, there is not such thing as "cross-UID collision problem" in Docker image. They run under one user only. The assumptions about
readOnlyRootFilesystemare also wrong. It doesn't affect any volume mounted.This is exactly why I said you have to understand the issue first yourself before you try to fix it. I won't be responding to any of the AI generated non-sense anymore here.