npm install fails in jenkins pipeline in docker

jenkins nodejs pipeline
jenkins npm install permission denied
jenkins docker pipeline
docker npm install fails
jenkins pipeline docker permission denied
docker node
docker failed to start 'npm
docker npm install hangs

I'm following a tutorial about Jenkins pipeline and I can get a "hello world" working under at node 6.10 docker container.

But, when I added a default EmberJS app (using ember init) to the repo and attempt to build that in the pipeline, it fails when running npm install (because of directory access issues). The Jenkinsfile can be seen here: https://github.com/CloudTrap/pipeline-tutorial/blob/fix-build/Jenkinsfile

The error message printed by the build is (which is installed locally and run using java -jar jenkins.war on a Macbook, not relevant but included just in case) is:

npm ERR! Linux 4.9.12-moby
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
npm ERR! node v6.10.0
npm ERR! npm  v3.10.10
npm ERR! path /.npm
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall mkdir

npm ERR! Error: EACCES: permission denied, mkdir '/.npm'
npm ERR!     at Error (native)
npm ERR!  { Error: EACCES: permission denied, mkdir '/.npm'
npm ERR!     at Error (native)
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/.npm',
npm ERR!   parent: 'pipeline-tutorial' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

Note: I would like to not run npm install as root / sudo.

UPDATE: I have been able to make some progress as follows:

I found the command that Jenkins uses to build using the container from the logs:

[Pipeline] withDockerContainer
$ docker run -t -d -u 501:20 -w /long-workspace-directory -v /long-workspace-directory:/long-workspace-directory:rw -v /long-workspace-directory@tmp:/long-workspace-directory@tmp:rw -e

So when the docker image runs, it's work directory is a /long-workspace-directory (it's really a cryptic looking jenkins workspace path) and the user id is 501 (group id 20), etc. The user doesn't have a name (which is apparently breaking other things not related to this question).

  1. Changed agent to use a Dockefile:

    agent {
      dockerfile {
        filename 'Dockerfile'
        args '-v /.cache/ -v /.bower/  -v /.config/configstore/'
      }
    }
    
  2. Specify args '-v ...' for creating volumes for the directories npm install / bower needs.

Adding the environments and setting the Home to '.' solves this as below.

pipeline {
    agent { docker { image 'node:8.12.0' } }
    environment {
        HOME = '.'
    }
    stages {
        stage('Clone') {
            steps {
                git branch: 'master',
                    credentialsId: '121231k3jkj2kjkjk',
                    url: 'https://myserver.com/my-repo.git'
            }
        }
        stage('Build') {
            steps {
                sh "npm install"
            }
        }
    }
}

Build in Jenkins failing with or without root (npm VS docker) · Issue , Build in Jenkins failing with or without root (npm VS docker) #9035 the -u root this first issue is fixed, but the build fail during npm install because its not root Just create a Jenkins pipeline with the generated Jenkinsfile. Manually, I executed a npm install file for the package.json file on server. Does anybody have an experience regarding to executing npm commands during Jenkins pipeline stage? Here is the both Dockerfile & Jenkinsfile that I used. Dockerfile # build stage FROM node:lts-alpine as build-stage WORKDIR /app COPY package*.json ./ RUN npm install

from https://github.com/jenkins-infra/jenkins.io/blob/master/Jenkinsfile

docker.image('openjdk:8').inside {
    /* One Weird Trick(tm) to allow git(1) to clone inside of a
    * container
    */
    withEnv([
        /* Override the npm cache directory to avoid: EACCES: permission denied, mkdir '/.npm' */
        'npm_config_cache=npm-cache',
        /* set home to our current directory because other bower
        * nonsense breaks with HOME=/, e.g.:
        * EACCES: permission denied, mkdir '/.config'
        */
        'HOME=.',
    ]) {
            // your code
    }
}

npm install fails in jenkins pipeline in docker, I'm following a tutorial about Jenkins pipeline and I can get a "hello world" working under at node 6.10 docker container. But, when I added a  Jenkins 2.46.1 in Docker on AWS EC2 t2.medium instance with Amazon Linux (amd64, 4.4.19-29.55.amzn1.x86_64) Node JS 6.10.2, npm 3.10.10 nodejs plugin 1.2.0 The only master is used to run jobs

Having wasted a whole day on this issue, I found simply adding the following as an environment variable at the agent stage using the Pipeline Editor removed the problem.

'npm_config_cache=npm-cache'

Build a Node.js and React app with npm, I'm following a tutorial about Jenkins pipeline and I can get a "hello world" working under at node 6.10 docker container. But, when I added a  If you use declarative pipelines for the build it`s no sense to install anything globally. If you install 'angular-cli' during the pipeline execution npm will set PATH variable for you and ng will be available. Global installation makes sense if you build your own image and plan to reuse it later.

Wanted to just provide a bit more detail, in short the accepted answer works but that I'm new to Docker and wanted to get a better understanding and figured i'd share what i found.

So for our jenkins setup, it starts containers via

docker run -t -d -u 995:315 -w /folder/forProject -v /folder/forProject:/folder/forProject:rw,z ...

As a result this container is running as user uid=995 gid=315 groups=315

Since the image I was using (circleci/node:latest) doesn’t have a user with this UID/GID, the user will not have a "home" folder and will only have permission on the mounted volume.

When NPM commands are called it will try using that users home directory (for cache) and since that user wasn’t created on the image, the home directory gets set to / (default for linux?). So to get NPM to work correctly we simply point the containers HOME environment variable for the user to the current folder via the Jenkins file

pipeline {
  agent none
  stages {
    stage('NPM Installs') {
      agent {
        docker {
            image 'circleci/node:latest'
        }
      }
      environment { HOME="." }
      ...
    }
  }
}

Thus giving the user the ability to create the required .npm folder in /folder/forProject/.npm

Hopefully this is helpful to someone and if you see something that i got wrong please let me know :D

Pipeline Steps Reference, You're now ready to create your Pipeline that will You'll have separate Jenkins and Node containers running locally in Docker. The Node container becomes the agent that  You should be able to set the NPM cache to a different location with npm config set cache --global /tmp/.npmcache/. Also, I'm not very happy that we set the user to root. And now that we have integrated jib, we should be using it to build the docker image without a docker daemon and directly push to a registry. So IMO our CI config is outdated.

I add the same issue. I solved it using the root user to run the Docker image:

node {
    stage("Prepare environment") {
        checkout scm
        // Build the Docker image from the Dockerfile located at the root of the project
        docker.build("${JOB_NAME}")
    }

    stage("Install dependencies") {
        // Run the container as `root` user
        // Note: you can run any official Docker image here
        withDockerContainer(args: "-u root", image: "${JOB_NAME}") {
            sh "npm install"
        }
    }
}

Jenkins build step fails when running npm install command, step([$class: 'DeployBuilder']) : Aliyun Container Service Deploy artifactoryNpmInstall : Run Artifactory npm install · artifactoryNpmPublish : Run Artifactory npm publish step([$class: 'GitHubIssueNotifier']) : Create GitHub issue on failure. Hello I use the docker slave plugin and when I try to run command "npm install" in node container I get next error: npm ERR! Linux 3.13.0-119-generic npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" npm ERR! node v7.10.0 npm ERR! npm v4.2.0 npm ERR! path /home/jenkins/.npm npm ERR! code EACCES npm ERR! errno -13 npm ERR

sudo permission inside docker container for jenkins pipeline, I am setting up a Jenkins build pipeline for vue application. I have a simple Dockerfile to build&run VUE application as a container. When I am trying to build the  I finally solved this by using Docker image layer caching for the npm install, following this answer. This means I moved the npm install out of the Docker slave image and into the actually frontend image, here is my final Docker file that truly caches the npm install in between builds if package.config has no changes:

Jenkins npm install permission denied, This is my sample Jenkinsfile. pipeline { agent { docker { image 'node:8.4' } } stages '/bin/sh -c npm install' returned a non-zero code: 1 Failed to build Docker  Allows pipelines to configure an NPM environment via managed npmrc files. Summary. This plugin provides integration with Pipeline by configuring an NPM environment to use within a pipeline job by calling sh npmor bat npm. This is accomplished by adding an npmrcas a custom config file to be centrally managed by Jenkins.

Jenkins npm install fails for directory A but is successful for directory B install Here, the npm binary provided by the node Docker container is used to install Set up continuous integration pipeline with Jenkins (this doc says for Ubuntu 16. js  On Fri, May 31, 2019 at 4:11 AM bharathkumar33 ***@***.***> wrote: Hello , I am trying to download the latest Jenkins docker image but when I want to install the selected plugins all the plugins install fails .

Comments
  • npm ERR! Please try running this command again as root/Administrator. try this hint.
  • ordinarily, you do not run npm install as root; it's considered really poor form; i'd like to avoid that.
  • Please take a look at nvm
  • I'm not installing any global dependencies. i use nvm extensively at work and for our existing ci setup.
  • i think I'm using the image incorrectly. the node image is for running​a node server application, not running a series of build commands.
  • wow, magic! This solved it for me too, thanks. I suppose npm depends on the HOME variable which isn't set by Jenkins.
  • the environment can be set in single stages, too. Thank you!
  • You only need to set HOME=. because the npm_config_cache is $HOME/.npm, so in this case it will be ./.npm instead of /.npm
  • I can confirm that just setting 'HOME=.' is enough
  • For completeness environment { HOME="." } fixes the problem for Jenkins declarative syntax.
  • I can confirm that just setting 'npm_config_cache=npm-cache' is enough as well
  • I am using yarn and have the same problem. Do you know how to solve it for yarn?
  • how to use this config?
  • After this you have to delete all files created under root user or jenkins will fail next jobs because of permissions conflict inside the workspace directory.
  • plus using root in containers is a bad security practice