sai harshini
sai harshini

Reputation: 51

jenkins pipeline nodeJs

My JenkinsFile script started throwing npm not found error. (it is working for maven but failing at npm)

    pipeline {
    environment {
    JENKINS='true'
     }
       agent any 
       stages{
    stage('change permissions') {
    steps {
        sh "chmod 777 ./mvnw "
    }
}

    stage('clean') {
    steps {
        sh './mvnw clean install'
    }
    }


    stage('yarn install') {
    steps{
        sh 'npm install -g yarn'
        sh 'yarn install'
    }
    }
    stage('yarn webpack:build') {
    steps {
        sh 'yarn webpack:build'
    }
    }

    stage('backend tests') {
    steps {
        sh './mvnw test'
    }
    }

    stage('frontend tests') {
    steps {
        sh 'yarn test'
    }
    }

    }
}

To fix that I am trying to setup NodeJs on my jenkins node. I installed the nodejs plugin and wrote the script

pipeline {
agent any

stages {
    stage('Build') {
        steps {
            nodejs(nodeJSInstallationName: 'Node 6.x', configId: '<config-file-provider-id>') {
                sh 'npm config ls'
            }
        }
    }
}
}

as shown in the https://wiki.jenkins.io/display/JENKINS/NodeJS+Plugin I also setup nodejs on global tools config

I also tried the solution in the installing node on jenkins 2.0 using the pipeline plugin

and it throws Expected to find ‘someKey "someValue"’ @ line 4, column 7. node { error. but I am still getting npm not found error on jenkins. I am new to jenkins so any help is appreciated. Thanks in advance

I was able to fix the issues. Followed the following link and was able to fix the issue. https://medium.com/@gustavo.guss/jenkins-starting-with-pipeline-doing-a-node-js-test-72c6057b67d4

Upvotes: 5

Views: 28289

Answers (2)

SegFault
SegFault

Reputation: 2220

If you need different version of Node.js and npm, you can install NodeJS plugin for Jenkins. Go to Manage Jenkins -> Global tools configuration and find NodeJS section. Select the version you need and name it as you prefer. You can also add npm packages that needs to be installed globally.

In a declarative pipeline, just reference the correct version of node.js to use:

stage('Review node and npm installations') {
  steps {
    nodejs(nodeJSInstallationName: 'node13') {
      sh 'npm -v'  //substitute with your code
      sh 'node -v'
    }
  }
}

Full example here: https://pillsfromtheweb.blogspot.com/2020/05/how-to-use-different-nodejs-versions-on.html

Upvotes: 6

Andre Mesquita
Andre Mesquita

Reputation: 918

Its a puzzle. ;)

Has a little reference trick.

You need to configure your jenkins to see your nodejs config name.

At Global Tool Configuration, you need define your node config name. It has reference to your Jenkinsfile reference.

Image from Gustavo Apolinario post at Medium

Look an Jenkingsfile adapted example with reference:

pipeline {
  agent any

  tools {nodejs "node"}

  stages {    
    stage('Cloning Git') {
      steps {
        git 'https://github.com/xxxx'
      }
    }        
    stage('Install dependencies') {
      steps {
        sh 'npm i -save express'
      }
    }     
    stage('Test') {
      steps {
         sh 'node server.js'
      }
    }             
  }
}

Complete case to study: Post at Medium by Gustavo Apolinario

Hope it helps!

Upvotes: 12

Related Questions