Search code examples
linuxjenkinsnpmjenkins-pipeline

npm: not found on jenkins agent, but available through ssh


I am trying to set up a jenkins pipeline that utilizes multiple agents. The agents are ubuntu instances living in a cloud tenancy (openstack). When trying to run some npm commands on some of the instances, I am getting the error npm: not found. I've read multiple other threads, but I am struggling to understand why npm might not be found. I set these instances up myself, and I know I installed all requirements, including node and npm.

Let's say I have 2 nodes - agent1 at IP1, and agent2 at IP2. They both have a user login with username cooluser1. When I do an ssh cooluser1@IP1 or ssh cooluser1@IP2, in either case, running npm -v gives me a proper node version (6.14.13). However, in my pipeline, npm is not found in the IP2 instance. Here's my pipline script:

pipeline {
    agent {
        node {
            label 'agent1'
        }
    }
    stages {
        stage('Build'){
            steps {
                sh 'hostname -I'
                sh 'echo "$USER"'
                sh 'echo "$PATH"'
                sh 'npm -v'
            }
        }
        stage ('Run Tests'){
            parallel {
                stage('Running tests in parallel') {
                    agent {
                        node {
                            label 'agent2'
                        }
                    }
                    steps {
                        sh 'hostname -I'
                        sh 'echo "$USER"'
                        sh 'echo "$PATH"'
                        sh 'npm -v'
                    }
                }
                stage {
                    // more stuff running on another agent (agent3)
                }
            }
        }
    }
}

As you can see, in both the main agent agent1, and in the parallel stages, I run the same code, which checks the host IP, the username, the path, and the npm version. The IPs are as expected - IP1 and IP2. The $USER in both cases is indeed cooluser1. The path looks something like this:

// agent1
+ echo 
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

// agent2
+ echo 
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

A bit strange, but identical in both cases.

However, when I get to npm --v, for agent1, I get a version number, and any npm commands I want to run are workoing. but in agent2, I get npm: not found, and the pipeline fails if I try to use any npm commands. The full error is here:

+ npm -v
/home/vine/workspace/tend-multibranch_jenkins-testing@tmp/durable-d2a0251e/script.sh: 1: /home/vine/workspace/tend-multibranch_jenkins-testing@tmp/durable-d2a0251e/script.sh: npm: not found

But I clearly saw with ssh cooluser1@IP2 that npm is available in that machine to that user.

What might be going wrong here?


Solution

  • I will propose to you to install nodejs plugin, configure any nodejs version you want in 'manage jenkins' -> 'global tools configurations' and set nodejs in pipeline:

    pipeline {
      agent any
      tools {
        nodejs 'NodeJS_14.17.1'
      }
      stages {
        stage ('nodejs test') {
          steps {
            sh 'npm -v'
          }
        }
      }
    }