Skip to content

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

Open

Description

@ViliusS

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

  1. MikeMcC399 commented on Jun 1, 2026

    @MikeMcC399
    Member

    This sounds very similar to cypress-io/cypress-docker-images#914 using a Cypress Docker image in a Jenkins environment.

  2. ViliusS commented on Jun 1, 2026

    @ViliusS
    Author

    @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.

  3. MikeMcC399 commented on Jun 2, 2026

    @MikeMcC399
    Member

    The following example Jenkins pipeline works without modifying the node:lts Docker image. Instead HOME = "${WORKSPACE}" is set as an environment variable.

    pipeline {
        agent {
            docker {
                image 'node:lts'
            }
        }
        
        environment {
            HOME = "${WORKSPACE}"
            NO_COLOR = 1
        }
    
        stages {
            stage('Checkout') {
                steps {
                    git branch: 'master',
                        url: 'https://github.com/jenkins-docs/simple-node-js-react-npm-app'
                }
            }
    
            stage('Build and Test') {
                steps {
                    sh 'npm ci'
                    sh 'npm test'
                    
                }
            }
        }
    }
    

    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 node Docker 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?

  4. changed the title [-]Broken NPM cache folder settings do not allow running image without root[/-] [+]Jenkins permissions issue running as non-root with npm[/+] on Jun 2, 2026
  5. ViliusS commented on Jun 2, 2026

    @ViliusS
    Author

    @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.

  6. changed the title [-]Jenkins permissions issue running as non-root with npm[/-] [+]Broken NPM cache folder settings do not allow running image without root[/+] on Jun 2, 2026
  7. added
    permissions
    on Jul 4, 2026
  8. Cdineshreddy12 commented on Nov 20, 2026

    @Cdineshreddy12

    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 --user UID, HOME resolves to /, hence the EACCES: mkdir /.npm. (Small correction to the issue's framing: it's not npm_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/.npm in the three templates plus the generated Dockerfiles. Verified against a locally built 24/bookworm-slim:

    • default root usage unchanged (node --version, npm --version, installs all fine)
    • npm install succeeds as UID 1002 (the Jenkins/rootless case from this issue)
    • npm install succeeds with --read-only --tmpfs /tmp as UID 1002 (the readOnlyRootFilesystem case, with the standard emptyDir-at-/tmp mount)
    • the node user (UID 1000) works as before, just caching under /tmp/.npm

    I've also written build-test.yml steps 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 /tmp raises concerns.

  9. ViliusS commented on Nov 21, 2026

    @ViliusS
    Author

    @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.

  10. Cdineshreddy12 commented on Nov 21, 2026

    @Cdineshreddy12

    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 readOnlyRootFilesystem case, and a per-UID path can't be expressed in a Dockerfile ENV — so /tmp is 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.

  11. ViliusS commented on Nov 21, 2026

    @ViliusS
    Author

    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 readOnlyRootFilesystem are 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    permissions

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions