1

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.

5
  • Check which user Jenkins is running as - whoami. Commented Nov 26, 2019 at 5:55
  • @DibakarAditya Hi, whoami results in jenkins. Btw I installed node under user ec2-user. Might this be the problem? Commented Nov 26, 2019 at 5:57
  • Apparently it is. Commented Nov 26, 2019 at 6:02
  • I see, am I supposed to tell jenkins to use the environment I set under another user? Commented Nov 26, 2019 at 6:14
  • Yes, provided the Jenkins user has appropriate permissions to the path. Commented Nov 26, 2019 at 6:27

1 Answer 1

1

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
        """
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.