Stan Luo
Stan Luo

Reputation: 3889

Jenkins declarative pipeline: npm command not found

So I have set up this jenkins ec2 instance, ssh into it, globally installed node and set PATH. But when executing my pipeline, it gives me npm command not found error.

I put echo $PATH in my pipeline and the result is:

/home/ec2-user/.nvm/versions/node/v10.15.1/bin:/sbin:/usr/sbin:/bin:/usr/bin

Which looks correct.

For reference, here's my very simple pipeline:

pipeline {
  agent { label 'master' }

  environment {
    PATH = "/home/ec2-user/.nvm/versions/node/v10.15.1/bin:${env.PATH}"
  }

  stages {
    stage('Test npm') {
      steps {
        sh """
          echo $PATH
          npm --version
        """
      }
    }
  }
}

Appreciate with any help.

Upvotes: 1

Views: 5186

Answers (1)

Stan Luo
Stan Luo

Reputation: 3889

As @Dibakar Adtya pointed, the problem is when jenkins executes a pipeline, it's under the user jenkins, whereas I configured node under another user, ec2-user, and jenkins doesn't have access to ec2-user's bin. Thank you @Dibakar!

A more elegant solution is to use Jenkins NodeJS Plugin. It saves you from the environment hassles. Now the pipeline is:

pipeline {
  agent { label 'master' }

  tools { nodejs "nodejs" }

  stages {
    stage('Test npm') {
      steps {
        sh """
          npm --version
        """
      }
    }
  }
}

Upvotes: 1

Related Questions